来自和大黄和楚哥的一次讨论
提问:运行后输出是什么?
public class StaticTest {
public static void main(String[] args){
StaticSuper sup = new StaticSub();
System.out.println(sup.staticGet());
System.out.println(sup.dynamicGet());
}
}
class StaticSuper{
public static String staticGet(){
return "Base staticget()";
}
public String dynamicGet(){
return "Base dynamic";
}
}
class StaticSub extends StaticSuper{
public static String staticGet(){
return "derived staticGet()";
}
public String dynamicGet(){
return "derived dynamic()";
}
}
答案:
Base staticget()
derived dynamic()
P.S:
1、StaticSuper sup = new StaticSub();
后sup向上转型,如StaticSub类中有父类中没有的方法,则sup没法用这个方法
2、静态方法不能多态