[英]How can i call child's static method from parent's static method?
I have a Base class 我有一个基类
public class BaseStatic {
public static String fname = "Base";
public static String lname = "Static";
public static void send(){
System.out.println("BaseStatic send");
sendTo();
}
public static void sendTo(){
//How to call from here Child's static method.
System.out.println("BaseStatic sendTo");
}
}
and I have a Child Class which extends it. 我有一个扩展它的Child Class。
public class FirstStatic extends BaseStatic {
public static String fname = "First";
public static String lname = "Static";
public static void sendTo(){
System.out.println("FirstStatic sendTo");
}
}
Now there is an Main class 现在有一个Main类
public class Main {
public static void main(String args[]){
FirstStatic.send();
}
}
Does java provide me a way so that when i call from Main method FirstStatic.send , It goes to send method of BaseStatic , from there i can call sendTo method of FirstStatic rather than calling sendTo method of BaseStatic java是否为我提供了一种方式,当我从Main方法FirstStatic.send调用时,它转到BaseStatic的send方法,从那里我可以调用FirstStatic的sendTo方法而不是调用BaseStatic的sendTo方法
There is no polymorphism for static methods. 静态方法没有多态性。 Therefore, in order to call a static method
x
of class A
, you must write Ax()
. 因此,为了调用类
A
的静态方法x
,必须编写Ax()
。
FirstStatic.send()
will call BaseStatic
's send
only because FirstStatic
has no static send
method. FirstStatic.send()
将调用BaseStatic
的send
,只是因为FirstStatic
没有固定send
方法。 However, BaseStatic
's send
will always call BaseStatic
's sendTo
, unless you explicitly call FirstStatic.sendTo()
. 然而,
BaseStatic
的send
总是会调用BaseStatic
的sendTo
,除非你明确地调用FirstStatic.sendTo()
The commented-out sendTo() method here would have the effect of hiding the implementation of sendTo() in the base class. 这里注释掉的sendTo()方法可以隐藏基类中sendTo()的实现。
public class BaseStatic {
public static String fname = "Base";
public static String lname = "Static";
public static void send(){
System.out.println("BaseStatic send");
sendTo();
}
public static void sendTo(){
//How to call from here Child's static method.
System.out.println("BaseStatic sendTo");
}
public static void main(String[] args) {
FirstStatic.sendTo();
}
}
class FirstStatic extends BaseStatic {
public static String fname = "First";
public static String lname = "Static";
//public static void sendTo(){
// System.out.println("FirstStatic sendTo");
//}
}
You can do the following, of course, but the net effect is identical to the code shown above. 当然,您可以执行以下操作,但净效果与上面显示的代码相同。 If you need polymorphism, you'll need to do away with the statics--probably a good idea, anyway!
如果你需要多态性,你需要取消静态 - 无论如何可能是一个好主意!
public class BaseStatic {
public static String fname = "Base";
public static String lname = "Static";
public static void send(){
System.out.println("BaseStatic send");
sendTo();
}
public static void sendTo(){
//How to call from here Child's static method.
System.out.println("BaseStatic sendTo");
}
public static void main(String[] args) {
FirstStatic.sendTo();
}
}
class FirstStatic extends BaseStatic {
public static String fname = "First";
public static String lname = "Static";
public static void sendTo(){
BaseStatic.sendTo();
}
}
EDIT: These examples are structured such that you can easily do the following to verify the behavior. 编辑:这些示例的结构使您可以轻松执行以下操作来验证行为。
javac BaseStatic.java
java BaseStatic
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.