[英]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.