class B
{
static void run(){
System.out.println("B");
}
}
class D extends C
{
static void run(){
System.out.println("D");
}
}
class C extends B
{ static void run(){
System.out.println("c");
}
public static void main(String[] args)
{
B b1 = new B();
b1.run();
B b2 = new C();
b2.run();
B b3 = new D();
b3.run();
C c1 = new C();
c1.run();
C c2 = new D();
c2.run();
}
}大家事先预测一下打印结果,再谈谈为什么会导致这样的原因!!!!
{
static void run(){
System.out.println("B");
}
}
class D extends C
{
static void run(){
System.out.println("D");
}
}
class C extends B
{ static void run(){
System.out.println("c");
}
public static void main(String[] args)
{
B b1 = new B();
b1.run();
B b2 = new C();
b2.run();
B b3 = new D();
b3.run();
C c1 = new C();
c1.run();
C c2 = new D();
c2.run();
}
}大家事先预测一下打印结果,再谈谈为什么会导致这样的原因!!!!
B
B
c
c静态成员函数不能重写
static 的问题,如果全部改成public 答案就对了。
静态方法不能被覆盖,在编译时就已经决定了该调用哪个方法了。并且静态方法建议、推荐用类名调用,因为它是属于类的,而不是属于对象的。
绑定在其声明类型上,而不是实际类型
b2引用不是指向C对象吗,怎么不运行C类中的run方法?反而执行B类中的run方法?
谁能解译一下其中的奥秒?
而且本来就应该这么写...
静态绑定就是编译时就绑定了