package com.guigu.exercise;
public class Person {
protected String name = “person”;
protected int age = 50;
public String getInfo(){
return “Name: “+name + “\n” + “age: “ + age;
}
}
**
package com.guigu.exercise;
public class Student extends Person{
protected String school = “pku”;
public String getInfo(){
return “Name: “ + name + “\nage: “ + age + “\nschool: “+ school;
}
}
**
package com.guigu.exercise;
public class Graduate extends Student{
public String major = “IT”;
public String getInfo(){
return “Name:” + name + “\nage:” + age + “\nschool: “+
school + “\nmajor:” + major ;
}
}
**
package com.guigu.exercise;
public class InstanceTest {
public static void main(String[] args) {
InstanceTest test = new InstanceTest();
test.method(new Student());
}
public void method(Person e){
//虚拟方法调用
String info = e.getInfo();
System.out.println(info);
if(e instanceof Graduate){
System.out.println(“a graduated student”);
System.out.println(“a student”);
System.out.println(“a person”);
}else if(e instanceof Student){
System.out.println(“a student”);
System.out.println(“a person”);
}else{
System.out.println(“a person”);
}
}
}