Dec*_*ver 3 java polymorphism overriding
我有一个简单的问题,我无法找到一个好的答案.为什么以下Java程序显示20?如果可能的话,我希望得到详细的回复.
class Something{
public int x;
public Something(){
x=aMethod();
}
public static int aMethod(){
return 20;
}
}
class SomethingElse extends Something{
public static int aMethod(){
return 40;
}
public static void main(String[] args){
SomethingElse m;
m=new SomethingElse();
System.out.println(m.x);
}
}
Run Code Online (Sandbox Code Playgroud)
因为多态只适用于实例方法.
这里调用的static方法aMethod
public Something(){
x=aMethod();
}
Run Code Online (Sandbox Code Playgroud)
指的是aMethod声明的Something.
| 归档时间: |
|
| 查看次数: |
480 次 |
| 最近记录: |