我最近看到了谷歌图书馆Gson的源代码,看到了一些非常奇怪的东西(IMO).在com.google.gson.internal包中,有两个类以"$"开头.
例如
public final class $Gson$Types { ... }
Run Code Online (Sandbox Code Playgroud)
美元符号是否对开发有影响,或者只是对外部开发人员说"请不要使用因为它是内部类"?
您可以在标识符中使用任何货币符号或继续符.
通过使用$,它表明它是生成的,例如由构建生成,或仅供内部使用.
如果您与生成的类冲突,它只会导致问题,例如
class A {
class B {
}
}
class A$B {
}
Run Code Online (Sandbox Code Playgroud)
报告
error: duplicate class: A.B
Run Code Online (Sandbox Code Playgroud)
编译器生成的类不以一个$可能是这样做的原因开始.
在Windows 7上,可以出现在类中的特殊字符是
public static void main(String... args) throws IOException {
for (char ch = 0; ch < Character.MAX_VALUE; ch++) {
if (Character.isJavaIdentifierStart(ch) && !Character.isLetter(ch))
System.out.println("interface " + ch + " { }");
}
}
interface $ { }
interface _ { }
interface ¢ { }
interface £ { }
interface ¤ { }
interface ¥ { }
interface € { }
Run Code Online (Sandbox Code Playgroud)
Java允许使用其他特殊字符,但不支持文件名.这可能取决于操作系统和可能的语言.