应该在引用该类中的静态字段时使用类名吗?

huangapple go评论113阅读模式
英文:

Should I use a class name when referencing a static field in that class?

问题

以下是翻译好的部分:

如果我有一个类,其中有一个静态字段,例如:

  1. public class Foo {
  2. public static int bar;
  3. }

...而我想在其声明的类内引用此字段。我知道以下两种方式都可以:

  1. public class Foo {
  2. public static int bar;
  3. public static int getBar() {
  4. return bar;
  5. }
  6. }

和:

  1. public class Foo {
  2. public static int bar;
  3. public static int getBar() {
  4. return Foo.bar;
  5. }
  6. }

是否包含类名像这样的方式之间是否有任何偏好或约定?任何帮助都将不胜感激。

英文:

If I have a class with a static field such as:

  1. public class Foo {
  2. public static int bar;
  3. }

...And I want to reference this field within its declared class. I am aware that both:

  1. public class Foo {
  2. public static int bar;
  3. public static int getBar() {
  4. return bar;
  5. }
  6. }

And:

  1. public class Foo {
  2. public static int bar;
  3. public static int getBar() {
  4. return Foo.bar;
  5. }
  6. }

work. Is there any preference or convention between whether or not to include the class name like that? Any help is appreciated.

答案1

得分: 2

实际上,这完全基于个人意见。您应该使用您喜欢的任何方法。两者都同样有效。但我想补充一点,如果您正在使用getter/setter,那么请将静态变量声明为私有的:

  1. public class Foo {
  2. private static int bar;
  3. public static int getBar() {
  4. return bar;
  5. }
  6. public static int setBar(int _bar) {
  7. bar = _bar;
  8. }
  9. }

由于getter和setter方法都是静态的,这确保它们只能与Foo类的静态变量一起使用。这也解决了可读性问题。

英文:

Actually, its totally opinion based. You should do whatever method you prefer. Both are equally granted. But I would like to add one point, if you are using getter/setter, then declare the static variable private:

  1. public class Foo {
  2. private static int bar;
  3. public static int getBar() {
  4. return bar;
  5. }
  6. public static int setBar(int _bar) {
  7. bar = _bar;
  8. }
  9. }

And as the both getter and setter methods are static, it ensures, they both definitely work with only static variables of Foo class. This solves readability problem too.

答案2

得分: 1

I agree with whatever being mentioned in the comments. Apart from this, I keep readability of the code in mind and then decide. Following code fragment gives an idea on the same:

  1. public class Engine {
  2. private static boolean isOn = false;
  3. public static void doWork1() {
  4. if (isOn) { // 检查引擎是否打开
  5. // 做一些工作
  6. }
  7. }
  8. public static void doWork2() {
  9. if (Engine.isOn) {
  10. // 做一些工作
  11. }
  12. }
  13. ...
  14. }

You can see that doWork2() method by using the class name increased the code readability even without adding any comment to the code.

英文:

I agree with whatever being mentioned in the comments. Apart from this, I keep readability of the code in mind and then decide. Following code fragment gives an idea on the same:

  1. public class Engine {
  2. private static boolean isOn = false;
  3. public static void doWork1() {
  4. if (isOn) { // check if engine is on
  5. // do some work
  6. }
  7. }
  8. public static void doWork2() {
  9. if (Engine.isOn) {
  10. // do some work
  11. }
  12. }
  13. ...
  14. }

You can see that doWork2() method by using the class name increased the code readability even without adding any comment to the code.

huangapple
  • 本文由 发表于 2020年8月3日 18:48:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63228050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定