强调
静态方法是属于类的,只存在一份,会被该类的所有对象共享
静态方法可以被子类继承,但是不可以被子类重写
class door{
}
class wood_Door extends door{
}
class math{
static public door getMes() {
return new door();
}
}
public class HelloWorld extends math {
public static wood_Door getMes() {
return new wood_Door();
}
public static void main(String[] args) {
math m=new HelloWorld();
System.out.println(m.getMes());
}
}
//输出为[email protected]
子类不能通过继承重写父类的静态方法,但是可以隐藏父类的方法,如下
class door{
}
class wood_Door extends door{
}
class math{
static public door getMes() {
return new door();
}
}
public class HelloWorld extends math {
public static wood_Door getMes() {
return new wood_Door();
}
public static void main(String[] args) {
HelloWorld m=new HelloWorld();
System.out.println(m.getMes());
}
}
//输出为[email protected]
原文地址:https://www.cnblogs.com/uestcman/p/9876310.html
时间: 2024-11-05 11:00:33