Java继承和组合代码

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实列

原文地址:https://www.cnblogs.com/cckong/p/13738426.html