编程语言
首页 > 编程语言> > Java 入门25 继承

Java 入门25 继承

作者:互联网

 

 

 

 

 

 

 Person类

package com.ITheima.d7_extends;

public class Person {
    private String name;
    private  int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
View Code

Student类继承Person类

package com.ITheima.d7_extends;

public class Student extends Person{
    public  void study(){
        System.out.println(getName()+"好好学习");
    }
}
View Code

Teacher类继承Person类

package com.ITheima.d7_extends;

public class Teacher extends Person {
    public void teach() {
        System.out.println(getName()+"开始教导学生");
    }
}
View Code

 

案例

 

标签:25,Java,入门,age,Person,extends,void,public,name
来源: https://www.cnblogs.com/lvlisn/p/16410591.html