首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

多态的有关问题

2011-11-29 
多态的问题classB{staticvoidrun(){System.out.println( B )}}classDextendsC{staticvoidrun(){System.

多态的问题
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();
}
}

大家事先预测一下打印结果,再谈谈为什么会导致这样的原因!!!!


[解决办法]
B
C
D
C
D
[解决办法]
B
B
B
c
c

静态成员函数不能重写
[解决办法]
还是不太懂,期待更详细答案
[解决办法]
b b b c c
静态方法不能被覆盖,在编译时就已经决定了该调用哪个方法了。

并且静态方法建议、推荐用类名调用,因为它是属于类的,而不是属于对象的。
[解决办法]
静态成员函数不能重写

[解决办法]
static的方法...静态绑定
绑定在其声明类型上,而不是实际类型

热点排行