结论:
子类可以继承父类中的静态方法,但是不能重(覆)写它。
原因:
静态方法不是后期绑定的,它早在编译期就绑定了,换句话说,这种方法不会进行多态的判断,且只与声明的类有关。即静态绑定不支持多态,动态绑定才支持多态。
测试代码:
public class DEMO{
public static void main(String[] args){
father f = new son();
f.show();
f.method();
}
}
class father{
static void show(){
System.out.println("父类中的静态方法");
}
void method(){
System.out.println("父类中的一般方法");
}
}
class son extends father{
static void show(){
System.out.println("子类中的静态方法");
}
void method(){
System.out.println("子类中的一般方法");
}
}
测试结果:
过程:
利用子类实例化父类对象,调用静态方法show()输出的结果为:父类的静态方法,证明子类继承了父类的静态方法而没有覆盖它,同时调用method方法时,输出为“子类中的一般方法”,可见此时子类中的一般方法覆写了父类中的一般方法。