本文介绍了为什么我不能使用“超级”来自静态上下文的变量,即使“超级”也是如此。指的是父类而不是类实例,不像“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 keyword:

Here is the section in the JLS about the super keyword:

表单 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”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:16