java 保留字作为名称或标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/423994/
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
Reserved words as names or identifiers
提问by Salty
Is there any tricky way to use Java reserved words as variable, method, class, interface, package, or enum constant names?
有什么巧妙的方法可以使用 Java 保留字作为变量、方法、类、接口、包或枚举常量名称吗?
回答by Rob Kennedy
This is a valid question. Such a thing ispossible in other languages. In C#, prefix the identifier with @(as asked before); in Delphi, prefix with &. But Java offers no such feature (partly because it doesn't really need to interact with identifiers defined by other languages the way the .Net world does).
这是一个有效的问题。这样的事情在其他语言中是可能的。在 C# 中,在标识符前加上@(如前所述);在 Delphi 中,前缀为&. 但是 Java 没有提供这样的特性(部分是因为它并不像 .Net 世界那样真正需要与其他语言定义的标识符交互)。
回答by Salty
No, there is no way. That's why they're labeled "reserved".
不,没有办法。这就是为什么它们被标记为“保留”的原因。
回答by starblue
Most often this issue comes up for "class", in this case it is customary to write "clazz".
大多数情况下,这个问题出现在“class”上,在这种情况下,习惯上写“clazz”。
回答by Vlad Gudim
Strictly speaking you can't, unless you get your hands on a buggy compiler implementation that doesn't adhere to the Java language spec.
严格来说你不能,除非你得到一个不符合 Java 语言规范的有缺陷的编译器实现。
But where there's a will, there's a way.Copy the following code into your IDE, switch the source file encoding to UTF-16 and here we go:
但只要有意愿,就有办法。将以下代码复制到您的 IDE 中,将源文件编码切换为 UTF-16,然后我们开始:
public class HelloWorld {
public static void main(String[] args) {
HelloWorld.nеw();
}
public static void nеw () {
System.out.println("Hello,World");
}
}
This code is a well-formed and valid Java class. However, as you have guessed there is a little trick: the 'е' character within "new" identifier does not belong to the ASCII character set, it is actually a cyrrilic 'е' (prounanced 'YE').
此代码是一个格式良好且有效的 Java 类。但是,正如您已经猜到的,有一个小技巧:“new”标识符中的 'е' 字符不属于 ASCII 字符集,它实际上是一个西里尔字母 'е'(发音为 'YE')。
Current Java language spec explicitly permits, and this an important point to make, the use of Unicode for naming identifiers. That means that one has an ability to freely call her or his classes in French, Chinise or Russian if they wish. It is also possible to mix and match the alphabets within code. And historically, some letters within Latin and other alphabets are lookalikes.
当前的 Java 语言规范明确允许使用 Unicode 来命名标识符,这一点很重要。这意味着一个人可以自由地用法语、中文或俄语给她或他的课程打电话,如果他们愿意的话。也可以在代码中混合和匹配字母。从历史上看,拉丁字母和其他字母表中的一些字母是相似的。
As a result: no, you can't use the reserved words as identifiers, but you can use identifiers that look exactly like reserved words.
结果:不,您不能使用保留字作为标识符,但您可以使用与保留字完全相同的标识符。
Whether anyone should be doing it is a totally different matter.
是否有人应该这样做是完全不同的问题。
回答by bruno conde
No, you can't do this. For more information please go to JLS Sections 3.8, 3.9
不,你不能这样做。有关更多信息,请参阅JLS 第 3.8、3.9 节
The following character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (§3.8):
Keyword: one of abstract continue for new switch assert default if package synchronized boolean do goto private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const float native super while
以下由 ASCII 字母组成的字符序列保留用作关键字,不能用作标识符(第 3.8 节):
Keyword: one of abstract continue for new switch assert default if package synchronized boolean do goto private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const float native super while
回答by Yoni Roit
Yes, there is. You have to use reserved words from the future. Like what happened with different methods called assert() in pre-1.4 code.
就在这里。你必须使用未来的保留字。就像 1.4 之前的代码中称为 assert() 的不同方法发生的情况一样。
Hope it helps!
希望能帮助到你!
回答by Miserable Variable
Huh? Why would you want to do that? You can write them in l33t, that will fool the compiler.
嗯?你为什么想这么做?您可以在 l33t 中编写它们,这会欺骗编译器。
class cl4ss {
String r3turn() {
return "but why?";
}
}
回答by SK16
I know it's old question still, might help someone.
我知道这仍然是个老问题,可能会对某人有所帮助。
It's possible by using GSON's Field Naming Support
可以通过使用 GSON 的字段命名支持
eg.
例如。
@SerializedName("new")
private String New;
public String getNew ()
{
return New;
}
public void setNew (String aNew)
{
New = aNew;
}
回答by Ira Baxter
PL/1 (a 1960's IBM mainframe programming language still around today) rather famously required that while some words act like keywords in certain contexts, all words can be used as identifiers. This isn't even that hard to do in a parser if you set out to be consistent about it. PL/1 was considered to a rather big langauge, and the langauge committee worried that many programmers wouldn't learn all of it, and then would get suprised when they tried to use the keyword from a part they didn't know as an identifier. So you could write things like:
PL/1(一种 1960 年代的 IBM 大型机编程语言,至今仍在使用)相当著名地要求,虽然某些词在某些上下文中充当关键字,但所有词都可以用作标识符。如果您开始对此保持一致,那么在解析器中这甚至不难做到。PL/1 被认为是一个相当大的语言,语言委员会担心许多程序员不会全部学习,然后当他们试图使用他们不知道的部分的关键字作为标识符时会感到惊讶. 所以你可以写这样的东西:
IF BEGIN=ELSE THEN CALL=3 ELSE CALL FOO(ENDIF) ENDIF
As others have noted here, the ability to do this isn't a recommendation.
正如其他人在此处指出的那样,执行此操作的能力不是建议。
The Java designers decided the number of keywords in the langauge was modest, and reserved the set. They even reserved 'GOTO', which isn't actually allowed in any real Java program.
Java 设计者决定语言中关键字的数量不多,并保留了该集合。他们甚至保留了“GOTO”,这实际上在任何真正的 Java 程序中都是不允许的。
回答by Hyman
In Scala you can use backticks. For example: myVarialbe.`class`
在 Scala 中,您可以使用反引号。例如:myVarialbe.`class`

