java 为什么我不能在静态上下文中使用“super”变量,即使“super”指的是父类而不是类实例,与“this”不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14112792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:06:39  来源:igfitidea点击:

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"?

javastaticthissuper

提问by PrashanD

I'm talking java language.

我说的是java语言。

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.

变量“this”在类中使用时,指的是该类的当前实例,这意味着您不能在静态方法中使用“this”。

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.

但是“super”在类中使用时,指的是该类的超类,而不是超类的实例,这意味着您可以在静态方法中使用“super”。但事实证明你不能。

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...

一个可能的解释是说“超级”也指的是超类的一个实例,但我不明白为什么它应该......

回答by Sean Owen

No, superdoes refer to an instance -- the same instance that thisrefers to -- the current object. It's just a way to reference methods and fields in defined in the superclass that are overridden or hidden in the current class.

不,super确实引用了一个实例——this引用了当前对象的同一个实例。这只是一种引用超类中定义的方法和字段的方法,这些方法和字段在当前类中被覆盖或隐藏。

回答by Sean Patrick Floyd

Here is the section in the JLS about the superkeyword:

这是 JLS 中有关super关键字的部分:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.11.2

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.11.2

The form super.Identifierrefers 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.

The form T.super.Identifierrefers 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.

表单super.Identifier引用了当前对象的名为Identifier的字段,但将当前对象视为当前类的超类的一个实例。

该表单T.super.Identifier引用与 T 对应的词法封闭实例的名为 Identifier 的字段,但该实例被视为 T 的超类的实例。

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.

此外,静态上下文与实例上下文有些不同,因为类不能覆盖静态方法,只能隐藏它们。

回答by Ted Hopp

You can't use superfrom a static context for the same reason you can't use thisin a static context. In both cases, the word refers to an instance.

您不能super在静态上下文中使用this,原因与不能在静态上下文中使用的原因相同。在这两种情况下,这个词都是指一个实例。

In a static context, you can always use the name of the superclass explicitly:

在静态上下文中,您始终可以显式使用超类的名称:

class Sub extends Base {
    static void func() {
        Base.func();
        . . .
    }
}

回答by Tmc

Super is a non static variable and non static entity cannot be accessed from static context.

Super 是一个非静态变量,不能从静态上下文访问非静态实体。