`
cynhafa
  • 浏览: 155127 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

子类已经覆盖了父类的静态方法,应该运行子类的静态方法才对呀?

 
阅读更多
package hidestatic;
class Base{
void method(){
system.out.println("method of Base");
}
static void staticMethod(){
System.out.println("static method of Base");
}
}
public class Sub extends Base{
void method(){
System.out.println("method of Sub");
}
static void staticMethod(){
System.out.println("static method of Sub");
}

public static void main(String args[]){
Base sub1=new Sub();
sub1.method();
sub1.staticMethod();

Sub sub2=new Sub();
sub2.method();
sub.staticMethod();
}
}

运行结果:method of Sub
static method of Base
method of Sub
static method of Sub


为什么:sub1.staticMethod()的运行结果是:static method of Base ? 按理说,子类已经覆盖父类的静态方法了,应该运行子类的静态方法才对呀,为什么还是调用父类的静态方法?
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics