为什么我不能使用“超级”来自静态上下文的变量,即使“超级”也是如此。指的是父类而不是类实例,不像“this”? [英] Why can I not use "super" variable from a static context, even though "super" refers to the parent class and NOT a class instance, unlike "this"?
问题描述
我说的是java语言。
I'm talking java language.
变量this,在类中使用时,指的是该类的当前实例,这意味着你不能使用静态方法中的this。
Variable "this", when used inside a class, refers to the current instance of that class, which means you cannot use "this" inside a static method.
但是超级,当在类中使用时,指的是该类的超类,而不是超类的实例,应该意味着你可以在静态方法中使用超级。但事实证明你不能。
But "super", when used inside a class, refers to the superclass of that class, not an instance of the superclass, which should mean that you can use "super" inside a static method. But it turns out you cannot.
一个可能的解释是说超级也指的是超类的一个实例,但我不明白为什么它应该......
A possible explanation would be to say that "super" also refers to an instance of the superclass, but I can't see why it should...
推荐答案
以下是JLS中有关 super $ c $的部分c> keyword:
Here is the section in the JLS about the super
keyword:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.11.2
表单
super.Identifier
是指
当前对象的名为Identifier的字段,但是当前对象被视为
的实例当前类的超类。
The form
super.Identifier
refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.
表单 T.super.Identifier
指的是
的字段,该字段是与T对应的词法封闭实例,但是该
实例被视为T的超类的实例。
The form T.super.Identifier
refers to the field named Identifier of
the lexically enclosing instance corresponding to T, but with that
instance viewed as an instance of the superclass of T.
在b在其他情况下,很明显需要一个实例对象。
In both cases, it is clear that an instance object is needed.
此外,静态上下文与实例有些不同上下文,因为类不能覆盖静态方法,只能隐藏它们。
Also, a static context is somewhat different from an instance context, as a class can't override static methods, only hide them.
这篇关于为什么我不能使用“超级”来自静态上下文的变量,即使“超级”也是如此。指的是父类而不是类实例,不像“this”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!