[英]Static method from superclass called on subclass
我需要在子类上调用超类静态方法,并且要知道它是在子类上调用的。 下面的代码显示了用例,请注意子类B.importantMethod()
上的方法调用以及我需要知道在子类上调用它的地方: //would like to get B here
class A {
public static void importantMethod() {
System.out.println("???"); //would like to get B here
}
}
class B extends A {
//no implementation, just inheritance
}
public class Test {
public static void main(String[] args) {
B.importantMethod();
}
}
实现这一目标的任何可能方式对我都是有益的。 我的尝试一无所获,我总是得到A
,而不是B
这是不可能的。 静态方法始终属于定义它们的类。 importantMethod
是A
类的方法。
静态方法是类方法,因此属于该类; 没有办法做你想做的。 有关更多说明,请参见本主题 。 如果您以这种方式提供有关正在尝试解决的问题的更多信息,也许我们可以帮助您找到替代的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.