gpt4 book ai didi

java - 根据 JLS(6.4.2。模糊)的 "obscuring"的代码示例,尤其是这个 "local variable or type can obscure a package"

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:04:35 24 4
gpt4 key购买 nike

你能举几个模糊的例子(代码片段)吗?

我读了JLS ,但我不明白这个概念。 JLS 没有给出代码示例。

隐藏在Base类和Derived类的字段之间。

阴影在字段和局部变量之间。

模糊 - 在什么 (?) 和什么 (?) 之间

旁白:有趣的是,JLS 说如果从父类中隐藏相应的字段不会继承:

Shadowing is distinct from hiding (§8.3, §8.4.8.2, §8.5, §9.3, §9.5), which applies only to members which would otherwise be inherited but are not because of a declaration in a subclass. Shadowing is also distinct from obscuring (§6.4.2).

我也知道类名、方法名和字段名都在它们不同的命名空间中:

// no problem/conflict with all three x's
class x {
void x() { System.out.println("all fine"); }
int x = 7;
}

示例:

终于找到一个example还有一些explanation据称是模糊的(它会导致编译错误):

class C {
void m() {
String System = "";
// Line below won't compile: java.lang.System is obscured
// by the local variable String System = ""
System.out.println("Hello World"); // error: out can't be resolved
}
}

The above situation could be resolved by using the fully qualified name java.lang.System, or by statically importing System.out. If we happened to also have variables called java and out, neither of these workarounds would work and it would be impossible to access System.out.println.

最佳答案

鉴于 JLS 中的解释:

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package.

一个简单的例子可以是:

int String = 0;
String name = "2";

//Will print 0 as per the JLS
//Would fail without the obscured String variable
System.out.println(String);

标识符 String 是类 java.lang.String 的简单名称,可以说在 String< 的声明中被隐藏了 局部变量。

关于java - 根据 JLS(6.4.2。模糊)的 "obscuring"的代码示例,尤其是这个 "local variable or type can obscure a package",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54112092/

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