我读过Java中的Statics不会被继承。我有一个小程序,在运行时编译并生成2 2
作为输出。从程序看来,k(一个静态变量)正在被继承!我究竟做错了什么?Java中的静态成员
class Super
{
int i =1;
static int k = 2;
public static void print()
{
System.out.println(k);
}
}
class Sub extends Super
{
public void show()
{
// I was expecting compile error here. But it works !!
System.out.println(" k : " + k);
}
public static void main(String []args)
{
Sub m =new Sub();
m.show();
print();
}
}
从来没有听说过,你的假设可能是错误的。 – 2010-01-29 09:42:00
我在这里阅读:http://stackoverflow.com/questions/1740528/inheritance-vs-static-in-java – gameover 2010-01-29 09:43:24
如果你阅读了关于这个的评论,那么这张海报正在将'继承'表示为'继承的能力用多态运行时调度来覆盖“,这有点不合常规,容易混淆。 – 2010-01-29 09:49:15