重写和隐藏有什么区别,特别是“静态方法不能在子类中被重写,只能被遮蔽”这句话
发布于 2013-06-18 01:43:44
如果您真的覆盖了一个方法,那么您可以在方法中的某个点调用super()来调用超类实现。但是由于static方法属于一个类,而不是一个实例,您只能通过提供一个具有相同签名的方法来“隐藏”它们。任何类型的静态成员都不能继承,因为它们必须通过类访问。
发布于 2013-06-18 01:51:03
重写...这个术语指的是多态行为,例如
class A {
method(){...} // method in A
}
class B extends A {
@Override
method(){...} // method in B with diff impl.
}当您尝试从类B调用该方法时,会得到重写的behvaior ..e.g
A myA = new B();
myB.method(); // this will print the content of from overriden method as its polymorphic behavior但是假设您已经使用静态修饰符声明了该方法,而不是通过尝试相同的代码
A myA = new B();
myA.method(); // this will print the content of the method from the class A这是因为静态方法不能被重写...类B中的方法只是隐藏了类A中的方法...
https://stackoverflow.com/questions/17153539
复制相似问题