//Person p = new Student(); // Person父类 Student子类
//System.out.println(p instanceof Person); // instanceof判断p对象是否能使用父类方法
// 继承是is关系,组合是has关系。
public class Object1 {
public static void main(String[] args) {
//Person p = new Person("小王",24);
//Student s = new Student("莉莉",21,100);
PrimaryStudent m = new PrimaryStudent("莉莉",21,100,5);
//System.out.println(p.getAge());
//System.out.println(s.getAge());
System.out.println(m.getScore());
}
}
class Person{
protected String name; // protected允许子类访问父类的字段和方法
protected int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
class Student extends Person{
protected int score;
public Student(String name,int age,int score){
super(name,age); // 调用父类的构造方法Person(String name,int age)
this.score = score;
}
public int getScore(){
return score;
}
}
class PrimaryStudent extends Student{
protected int grade;
public PrimaryStudent(String name,int age,int score,int grade){
super(name,age,score);
this.grade = grade;
}
public int getGrade(){
return grade;
}
}