8.2 类继承定义

发布时间 2023-06-07 14:55:17作者: 盘思动
// class 子类 extentd 父类 {}
// 很多情况下: 会把子类称为派生类,把父类称为超类(superCall)
class Person {
    private String name;
    private int age;
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;    
    }
}

class Student extends Person {
    private String school;
    public void setSchool(String school){
        this.school = school;
    }
    public String getSchool(){
        return this.school;
    }
}

public class HelloWorld {
    public static void main(String args[]){
        Student stu = new Student();
        stu.setName("李寻欢");// 子类可以直接调用父类中方法
        stu.setAge(22);
        stu.setSchool("清华大学");
        System.out.println("姓名:" + stu.getName() + ",年龄:" + stu.getAge() + ",学校:" + stu.getSchool());
    }

}