首页 > 技术文章 > Java继承和组合代码

cckong 2020-09-27 11:28 原文

class Student extends Person {
    protected int score;

    public Student(String name, int age, int score) {
        super(name, age); // 调用父类的构造方法Person(String, int)
        this.score = score;
    }
}

继承为 extends

里氏替换原则通俗的来讲就是:子类可以扩展父类的功能,但不能改变父类原有的功能。

Person p = new Student(); 

父类可以引用子类的实例

 

class Student extends Person {
    protected Book book;
    protected int score;
}

student 继承person 里面组合了Book实列

推荐阅读