class A {
void fun1() {
System.out.println(fun2());
}
int fun2() {
return 345;
}
}
//B继承了A,首先B就有了和A一样的那些非private属性和方法
public class B extends A {
//自己又定义一个fun2(),则把A的那个复写了
int fun2() {
return 678;
}
public static void main(String args[]) {
B b = new B();
b.fun1();
//这里的a的编译时类型是A,但运行时是让他指向了b指向的对象,所以运行时是和b一样的东东
A a = b;
a.fun1();
}
} |