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'.
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 编译器来包含上下文规则,该规则允许您使用 class 作为变量名。尽管我认为仅使用 clazz 或 klass 会更好地利用您的时间,但它也可能对您的健康有益。
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 inability to 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 reserved word. That is as simple and as low-level as it gets.
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.
因为可以说 Class 对象 代表一个 type,我个人更喜欢将此类变量声明为Class type。
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.
As it is possible to say that a Class object represents a type, I personally prefer to declare such variables as Class type.
它只是将一个英文单词替换(等同于)Keyword Class Keyword,让人们明白它是一个Class。这几乎是为了增加代码的可读性,
其中没有涉及任何大的逻辑
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
发布评论
评论(9)
自 JDK 1.0 起,
clazz
就在 Java 中用来代替保留字“class”。 “class” 是您想要的,但缩写或插入垃圾(“a”、“the”、“_”等)会降低清晰度。clazz
只是说类。 “国际”英语使用者(同时阅读英式英语和美式英语的人)习惯于调换“s”和“z”。由于 Java 从一开始就公开了源代码和合适的文化,因此有价值的 Java 代码和教程采用了相同的约定。这是 Java 生态系统的伟大之处之一,我认为这是其成功的重要组成部分。
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'.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.
因为他们不能使用他们想要使用的词,即
class
。它是保留的。Because they cannot use the word they want to use which is
class
. It is reserved.这只是因为“class”是保留关键字,因此不允许使用
Class class
。因此,您将看到Class clazz
或Class cls
。It's simply because 'class' is a reserved keyword, hence
Class class
isn't allowed. Therefore you'll seeClass clazz
orClass cls
.这取决于实际的编译器及其区分令牌在其上下文中的含义的能力。然而,在这种特殊情况下,编译器无法区分标记
class
在不同上下文中的含义。这是一条硬性规定,无论其上下文如何,class
都用于表示类的声明,因此它是一个保留词。这就是最简单、最底层的事情。如果您觉得有必要,您可以编写自己的 Java 编译器来包含上下文规则,该规则允许您使用
class
作为变量名。尽管我认为仅使用 clazz 或 klass 会更好地利用您的时间,但它也可能对您的健康有益。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 inability to distinguish what the token
class
means in a different context. It is a hard and fast rule thatclass
, regardless of its context, is used to denote the declaration of a class, and as such it is a reserved word. That is as simple and as low-level as it gets.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 useclazz
orklass
-- it would probably be good for your health as well.声明
Class clazz
在Java世界中很流行,但对于新手和拼写检查者来说可能会很尴尬。我听到有些人说,根据最小惊讶原则应该避免这种情况。因为可以说
Class
对象 代表一个 type,我个人更喜欢将此类变量声明为Class type
。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.As it is possible to say that a
Class
object represents a type, I personally prefer to declare such variables asClass type
.我第一次看到它是在 Josh Bloch 的益智游戏中。但我很确定其他开发人员很早就使用过它。乔什·布洛赫 (Josh Bloch) 让它变得更加出名。
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.
Java 没有允许您使用关键字作为标识符的功能,这与带有
@
前缀的 C# 不同(例如@class
是有效的标识符。)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.)它只是将一个英文单词替换(等同于)Keyword Class Keyword,让人们明白它是一个Class。这几乎是为了增加代码的可读性,
其中没有涉及任何大的逻辑
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
Nothing big Logic involved in this
我们使用 clazz 因为类是一个类型头。例如,您在此处使用 class:
和此处:
因此不幸的是,我们必须使用替代名称,例如 clazz。
We use clazz because class is a type header. For example, you use class here:
and here:
So unfortunately, we have to use alternate names such as clazz.