为什么 Java 程序员喜欢将变量命名为“clazz”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2529974/
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
Why do Java programmers like to name a variable "clazz"?
提问by Sawyer
I've seen lots of codes have declaration like Class clazz
, where does this originate from ? Is this some kind of convention ? I think 'clazz' is not even an English word , has no meaning at all , how can so many programmers name a wrong name coincidentally ?
我见过很多代码都有这样的声明Class clazz
,这来自哪里?这是某种约定吗?我觉得 'clazz' 连英文都不是,根本没有意义,这么多程序员怎么会巧合起一个错误的名字呢?
采纳答案by Tom Hawtin - tackline
clazz
has been used in Java in place of the reserved word "class" since JDK 1.0. "class" is what you want, but abbreviating or inserting junk ("a", "the", "_", etc) reduces clarity. clazz
just says class. "International" English speakers (those reading both British and American English) are used to transposing 's' and 'z'.
clazz
自 JDK 1.0 以来,已在 Java 中用于代替保留字“类”。“class”是你想要的,但缩写或插入垃圾(“a”、“the”、“_”等)会降低清晰度。clazz
只说课。“国际”英语使用者(那些阅读英式和美式英语的人)习惯于将 's' 和 'z' 移调。
Since Java has had disclosed source and a suitable culture right from the start, worthwhile Java code and tutorials pick up the same conventions. That's one of the great things about the Java ecosystem, which I think has been an important part of its success.
由于 Java 从一开始就公开了源代码和合适的文化,有价值的 Java 代码和教程采用了相同的约定。这是 Java 生态系统的一大优点,我认为这是其成功的重要组成部分。
回答by President James K. Polk
Because they cannot use the word they want to use which is class
. It is reserved.
因为他们不能使用他们想使用的单词 which is class
。它是保留的。
回答by sfussenegger
It's simply because 'class' is a reserved keyword, hence Class class
isn't allowed. Therefore you'll see Class clazz
or Class cls
.
这仅仅是因为 'class' 是一个保留关键字,因此Class class
是不允许的。因此,您将看到Class clazz
或Class cls
。
回答by gmhk
It is just a English word replaced(Equavalent) by Keyword Class Keyword, to make people understand that it is a Class. and it is almost to increase the readability of the Code
只是一个英文单词被Keyword Class Keyword代替(等价),让人明白它是一个Class。几乎是为了增加代码的可读性
Nothing big Logic involved in this
这没有什么大的逻辑
回答by Roman
where does this originate from ?
这是从哪里来的?
I saw it first at Josh Bloch's puzzlers. But I'm pretty sure it was used much earlier by other developers. Josh Bloch just made it more famous.
我第一次看到它是在 Josh Bloch 的拼图游戏中。但我很确定它被其他开发人员使用得更早。Josh Bloch 让它更出名。
回答by finnw
Java does not have a feature that allows you to use a keyword as an identifier, unlike C# with its @
prefix (e.g. @class
is a valid identifier.)
Java 没有允许您使用关键字作为标识符的功能,这与 C# 的@
前缀不同(例如@class
是有效标识符)。
回答by John
It comes down to the actual compiler and its ability to distinguish what a token means within its context. However, in this particular case, it is the compiler's inabilityto distinguish what the token class
means in a different context. It is a hard and fast rule that class
, regardless of its context, is used to denote the declaration of a class, and as such it is a reservedword. That is as simple and as low-level as it gets.
它归结为实际的编译器及其区分标记在其上下文中的含义的能力。但是,在这种特殊情况下,编译器无法区分令牌class
在不同上下文中的含义。class
无论上下文如何, 都用于表示类的声明,因此它是一个保留字,这是一个硬性规定。这很简单,也很底层。
If you feel compelled, you could write your own Java compiler to include a contextual rule that will allow you to use class
as a variable name. Though I think it would be far better use of your time to just use clazz
or klass
-- it would probably be good for your health as well.
如果您觉得有必要,您可以编写自己的 Java 编译器来包含允许您class
用作变量名的上下文规则。虽然我认为最好利用你的时间,clazz
或者klass
——它可能对你的健康也有好处。
回答by Romejanic
We use clazz because class is a type header. For example, you use class here:
我们使用 clazz 是因为 class 是一个类型头。例如,您在这里使用 class:
public class HelloWorld {
and here:
和这里:
Object.class.getName();
So unfortunately, we have to use alternate names such as clazz.
所以不幸的是,我们不得不使用替代名称,例如 clazz。
回答by Michal Kordas
Declaration Class clazz
is popular in Java world, but it may be awkward for newcomers and spellcheckers. I've heard some people saying that it should be avoided according to principle of least astonishment.
声明Class clazz
在 Java 世界中很流行,但对于新手和拼写检查者来说可能会很尴尬。我听有人说,根据最少惊讶的原则,应该避免这种情况。
As it is possible to say that a Class
object represents a type, I personally prefer to declare such variables as Class type
.
因为可以说一个Class
对象代表一个类型,所以我个人更喜欢将这样的变量声明为Class type
.
回答by user1843640
Simply put, class
has class that clazz
doesn't.
简单地说,class
有clazz
没有的类。