Java中的变量命名约定
在PHP中,我们(至少是优秀的程序员)总是以小写字母开头通用变量名称,但类变量/对象以大写字母开头以区分它们。 以同样的方式,我们以小写字母开头通用文件名,但包含类的文件以大写字母开头。
例如:
<?php
$number = 123;
$string = "a string";
$colors_array = array('red', 'blue', 'red');
$Cat = New Cat();
?>
Java 中的约定是否相同,即对象以大写开头,但其余部分以小写开头,还是像我在其他地方读到的那样,所有内容都以小写开头?
In PHP, we (at least the good programmers) always start general variable names with a lower-case letter, but class variables/objects with an uppercase letter to distinguish them. In the same way we start general file names with a lower case letter, but files containing Classes with an upper case letter.
E.g.:
<?php
$number = 123;
$string = "a string";
$colors_array = array('red', 'blue', 'red');
$Cat = New Cat();
?>
Are the conventions the same in Java, i.e., objects starting with uppercase, but the rest with lowercase, or does everything start with lowercase as I've read in other places?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在 Java 代码约定 中找到命名方式。
快速总结:
You can find the naming in the Java Code Conventions.
A quick summary:
一般来说,所有变量都以小写开头:
有些人喜欢在所有情况下都放置静态常量:
没有真正的标准
当您处理首字母缩略词时,事情会变得更烦人,并且对于是否应该使用:或
现在,无论哪种方式,请注意类名(Object、String、HTMLHandler)始终以大写字母开头,但各个对象变量以小写字母开头。
Generally, all variables will start with lower case:
Some people like to put static constants in all case:
Things get more annoying when you deal with acronyms, and there is no real standard on whether you should use:
or
Now, either way, note that the class names (Object, String, HTMLHandler) always start with a capital letter, but individual object variables start lowercase.
约定是类名以大写字母开头。 变量名称为小驼峰式。 即使变量引用一个对象,它仍然以小写开头。
此页面应该有所帮助。
The convention is that class names start with an uppercase letter. Variable names are lower camel case. Even if the variable references an object, it still starts with lower case.
This page should help.
这些约定实际上取决于您编码的具体位置。
一般来说,从我所看到的类是大驼峰式(大写在前) ,方法以小写字母开头,变量我到处都见过(一些大写驼峰式,一些 小驼峰式 首字母较低(如之前的答案中所述,这是常态),有些甚至带有 匈牙利表示法)。 这取决于您的风格以及您正在从事的项目采用的风格。
The conventions really depend on the individual place you're coding for.
In general, from what I've seen classes are upper camel cased (with upper case first), methods start with lower case, and variables I've seen all over the place (some upper camel cased, some lower camel cased with first letter lower (as mentioned in previous answers, this is the norm), some even with Hungarian notation). It depends on your style and the style that the project you're working on has adopted.
有些人(不是我)喜欢区分方法变量和实例变量,通过在实例变量前加上“this”来实现。 这也解决了将参数分配给同名的实例变量时出现的问题:
但是有些人觉得你应该始终使用该模式 - 但我并不对此疯狂 - 我认为如果你的方法和类保持很小,那就太过分了。
此外,有些人使用参数命名模式。 当您从构造函数分配给实例变量时,这(再次)会派上用场:
通常模式是
pVariable
或_variable
。 我偶尔会使用它,因为我发现它比这个更具可读性,但它的缺点是使 Javadoc 的可读性较差。无论如何,我真的不喜欢总是使用这些模式中的任何一个。 很高兴了解它们,但如果您确实需要帮助在整个代码中区分它们,请告诉 Eclipse 以不同的颜色显示它们。
Some people (who are not me) like to differentiate method variables from instance variables by prefixing instance variables with "this." This also fixes the problem that occurs when assigning a parameter to an instance variable of the same name:
But then some people feel you should always use that pattern—but I'm not crazy about it—I think it's overkill if you keep your methods and classes small.
Also, some people use a naming pattern for parameters. This (again) comes in handy when you are assigning from a constructor to an instance variable:
Usually the pattern is
pVariable
or_variable
. I occasionally use this because I find it more readable than this, but it has the disadvantage of making your Javadocs less readable.In any case, I don't really like the idea of always using any of these patterns. They are great to know, but if you really need help differentiating them throughout your code, tell Eclipse to show them in different colors.
您对使用 小驼峰大小写 作为实例方法和 静态的大驼峰式(帕斯卡大小写)?
我认为这可能非常有帮助,因为在验证哪个方法是静态时不会出现任何问题。 这是我的提议。
What do you think about using lower camel case for instance methods and upper camel case (Pascal case) for static?
I think it could be very helpful, because there wouldn't be any issues while verifying which method is static. It's my proposal.