package Father;
public class Duotai {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new f();
//可以执行的引用类型就不确定了:父类的引用指向子类
//student 能调用的方法都是自己的或者继承父类的!
Z s1 = new Z();
//F 父类型,可以指向子类,但是不能调用子类的独有的方法
F s2 = new Z();
Object s3 = new Z();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
s2.run();//子类重写了父类的方法,执行子类的方法
s1.eat();
((Z)s2).eat();//强制类型转换
}
}
package Father;
public class Z extends F {
public void run() {
System.out.println("son");
}
public void eat() {
System.out.println("eat");
}
}
package Father;
public class F {
public void run() {
System.out.println("Father");
}
}