为什么Java程序员喜欢将变量命名为“clazz”?

发布于 2024-08-27 04:28:05 字数 1431 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

暮年 2024-09-03 04:28:05

自 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.

不离久伴 2024-09-03 04:28:05

因为他们不能使用他们想要使用的词,即class。它是保留的。

Because they cannot use the word they want to use which is class. It is reserved.

司马昭之心 2024-09-03 04:28:05

这只是因为“class”是保留关键字,因此不允许使用 Class class 。因此,您将看到 Class clazzClass cls

It's simply because 'class' is a reserved keyword, hence Class class isn't allowed. Therefore you'll see Class clazz or Class cls.

失眠症患者 2024-09-03 04:28:05

这取决于实际的编译器及其区分令牌在其上下文中的含义的能力。然而,在这种特殊情况下,编译器无法区分标记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 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.

[旋木] 2024-09-03 04:28:05

声明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 as Class type.

不回头走下去 2024-09-03 04:28:05

这是从哪里来的?

我第一次看到它是在 Josh Bloch 的益智游戏中。但我很确定其他开发人员很早就使用过它。乔什·布洛赫 (Josh Bloch) 让它变得更加出名。

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.

北陌 2024-09-03 04:28:05

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.)

暖树树初阳… 2024-09-03 04:28:05

它只是将一个英文单词替换(等同于)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

带刺的爱情 2024-09-03 04:28:05

我们使用 clazz 因为类是一个类型头。例如,您在此处使用 class:

public class HelloWorld {

和此处:

Object.class.getName();

因此不幸的是,我们必须使用替代名称,例如 clazz。

We use clazz because class is a type header. For example, you use class here:

public class HelloWorld {

and here:

Object.class.getName();

So unfortunately, we have to use alternate names such as clazz.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文