从静态方法调用超级方法
是否可以从子静态方法调用超级静态方法?
我的意思是,以通用的方式,到目前为止,我有以下内容:
public class BaseController extends Controller {
static void init() {
//init stuff
}
}
public class ChildController extends BaseController {
static void init() {
BaseController.loadState();
// more init stuff
}
}
并且它有效,但我想以通用的方式执行此操作,例如调用 super.loadState() ,这似乎不起作用。 ..
Is it possible to call a super static method from child static method?
I mean, in a generic way, so far now I have the following:
public class BaseController extends Controller {
static void init() {
//init stuff
}
}
public class ChildController extends BaseController {
static void init() {
BaseController.loadState();
// more init stuff
}
}
and it works, but I'd like to do it in a generic way, something like calling super.loadState(), which doesn't seem to work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在Java中,静态方法不能被重写。 此处清楚地解释了原因
因此,它不依赖于它所引用的对象。但相反,它取决于引用的类型。因此,静态方法被称为隐藏另一个静态方法而不是覆盖它。
例如(Cat 是 Animal 的子类):
主类:
现在,输出如下所示
对于
类方法
,运行时系统调用引用的编译时类型中定义的方法调用该方法的对象。换句话说,对静态方法的调用是在编译时映射的,并且取决于引用的声明类型(在本例中为 Parent),而不是运行时引用所指向的实例。在示例中,
myAnimal
的编译时类型为Animal
。因此,运行时系统调用Animal
中定义的hide方法。In Java, static methods cannot be overidden. The reason is neatly explained here
So, it doesn't depend on the object that it is being referenced. But instead, it depends on the type of reference. Hence, static method is said to hide another static method and not override it.
For example (Cat is a subclass of Animal):
Main class:
Now, the output would be as given below
For
class methods
, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called.In other words, call to static methods are mapped at the compile time and depends on the declared type of the reference (Parent in this case) and not the instance the reference points at runtime. In the example, the compile-time type of
myAnimal
isAnimal
. Thus, the runtime system invokes the hide method defined inAnimal
.Java 中有静态继承。改编 Nikita 的示例:
现在可以编译,当然,调用 C 会打印“CA”。现在我们将类 B 更改为:
并仅重新编译 B(而不是 C)。现在再次调用 C,它将打印“CB”。
不过,静态方法没有
super
之类的关键字 - 一个(坏的)理由可能是“超类的名称写在此类的声明中,因此您必须重新编译您的类”尽管如此,为了改变它,所以你也可以在这里改变静态调用。”There is static inheritance in Java. Adapting the example from Nikita:
This now compiles, and invoking C prints "CA", of course. Now we change class B to this:
and recompile only B (not C). Now invoking C again, it would print "CB".
There is no
super
like keyword for static methods, though - a (bad) justification may be that "The name of the super class is written in the declaration of this class, so you had to recompile your class nevertheless for changing it, so you could change the static calls here, too."整个继承概念不适用于 Java 中的静态元素。例如,静态方法不能覆盖另一个静态方法。
所以,不,您必须按名称调用它或使它们成为某个对象的实例方法。 (您可能特别想查看其中一种工厂模式)。
实际示例
这将打印
A
,然后打印B
。即,调用的方法取决于变量的声明方式,而不是其他。The whole inheritance concept isn't applied to static elements in Java. E.g., static method can't override another static method.
So, no, you'll have to call it by name or make them instance methods of some object. (You might want to check out one of factory patterns in particular).
A practical example
This prints
A
and thenB
. I.e., invoked method depends on how variable is declared and nothing else.如果您知道方法名称及其参数,您实际上可以以通用方式调用超类的静态方法。
这应该可行,并且在子类中,您可以使用基类中设置的所有内容。
输出应该是:
Hello from Base
myVar = Good
You can actually call the static method of a superclass in a generic way, given that you know the method name and its parameters.
This should work and in the subclass you have everything set in the base class available.
The output should be:
Hello from Base
myVar = Good
您的实现的正式名称称为方法隐藏。我建议引入静态 init(Controllercontroller) 方法,并调用实例方法来利用重写。
然后您可以调用Controller.init(controllerInstance)。
The official name of your implementation is called method hiding. I would suggest introducing a static init(Controller controller) method, and calling an instance method to take advantage of overriding.
You can then call Controller.init(controllerInstance).
对于静态方法,不需要类的实例,因此没有 super。
For static methods there is no instance of a class needed, so there is no super.