gpt4 book ai didi

java - 简单名称和限定名称

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:51:23 25 4
gpt4 key购买 nike

我正在阅读 JLS 8 and in Chapter 6写成:

A qualified name N.x may be used to refer to a member of a package or reference type, where N is a simple or qualified name and x is an identifier. If N names a package, then x is a member of that package, which is either a class or interface type or a subpackage. If N names a reference type or a variable of a reference type, then x names a member of that type, which is either a class, an interface, a field, or a method.

所以我可以想象拥有这个:

class C
{
public int n;
}

int j;
C c = new C();
j = 11;
c.n = 11;

j 是一个简单名称,而 c.n 是一个限定名称。

然而在6.2事情变得复杂了。给出此代码:

class Test { 
public static void main(String[] args) {
Class c = System.out.getClass();
System.out.println(c.toString().length() +
args[0].length() + args.length);
}
}

然后说:

The occurrence of length in args.length is a name because args.length is a qualified name (§6.5.6.2) and not a field access expression (§15.11). A field access expression, as well as a method invocation expression, a method reference expression, and a qualified class instance creation expression, uses an identifier rather than a name to denote the member of interest. Thus, the occurrence of length in args[0].length() is not a name, but rather an identifier appearing in a method invocation expression.

所以我想我明白并不是所有的表达式都是限定名称并且甚至我的表达 c.n

老实说,我不明白其中的区别,谁能帮帮我?

最佳答案

在你的例子中:

int j; 是一个简单的表达式名称,因为它由一个标识符组成

对于限定示例,来自 JLS 6.5.6.2. Qualified Expression Names :

If an expression name is of the form Q.Id, then Q has already been classified as a package name, a type name, or an expression name.

c.n中,c是一个表达式名,n是类T(在你的例子中是类C)的字段。所以 c.n 是一个合格的表达式名称。

args.length 也是一个限定的表达式名称。 args 是一个数组,因此它并不是一个真正的特定类(没有 .class 文件,而是 run-time type signature is generated though )但它仍然是一个具有名为 length 的字段的对象。

args[0].length() 不是限定名称,因为 length() 不是该类的成员。它是方法调用表达式的标识符。

MethodInvocation:
MethodName ( [ArgumentList] )
TypeName . [TypeArguments] Identifier ( [ArgumentList] )
ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
Primary . [TypeArguments] Identifier ( [ArgumentList] )
super . [TypeArguments] Identifier ( [ArgumentList] )
TypeName . super . [TypeArguments] Identifier ( [ArgumentList] )

ArgumentList:
Expression {, Expression}

比照。 https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12

关于java - 简单名称和限定名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40805981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com