重写
public class Father {
public static void tt() {
System.out.println("father");
}
public static void main(String[] args) {
Father a = new Son();
}
}
class Son extends Father{
public static void tt() {
System.out.println("son");
}
}
这是可以的,如果去掉static就报错
而且运行结果是print father
也就是说调用的还是父类的方法,可是明明是多态啊,这是为什么呢
重载
public class Father {
public static void tt() {
System.out.println("father");
}
public static void main(String[] args) {
Father a = new Son();
a.tt();
}
}
class Son extends Father{
public void tt(int a) {
System.out.println("son");
}
}
静态方法可以重载,而且相比于重写,不需要加static
实际调用的过程中,调用的也是父类的方法
其实在之前的文章中也看到,对于重载而言,多态是不会实现的。