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

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

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

问题

以下是翻译好的部分:

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

public class Foo {
    public static int bar;
}

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

public class Foo {
    public static int bar;

    public static int getBar() {
        return bar;
    }
}

和:

public class Foo {
    public static int bar;

    public static int getBar() {
        return Foo.bar;
    }
}

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

英文:

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

public class Foo {
    public static int bar;
}

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

public class Foo {
    public static int bar;

    public static int getBar() {
        return bar;
    }
}

And:

public class Foo {
    public static int bar;

    public static int getBar() {
        return Foo.bar;
    }
}

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,那么请将静态变量声明为私有的:

public class Foo {
    private static int bar;

    public static int getBar() {
        return bar;
    }
    public static int setBar(int _bar) {
        bar = _bar;
    }
}

由于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:

public class Foo {
    private static int bar;

    public static int getBar() {
        return bar;
    }
    public static int setBar(int _bar) {
        bar = _bar;
    }
}

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:

public class Engine {
    private static boolean isOn = false;

    public static void doWork1() {
        if (isOn) { // 检查引擎是否打开
            // 做一些工作
        }
    }

    public static void doWork2() {
        if (Engine.isOn) {
            // 做一些工作
        }
    }
    ...
}

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:

public class Engine {
    private static boolean isOn = false;

    public static void doWork1() {
        if (isOn) { // check if engine is on
            // do some work
        }
    }

    public static void doWork2() {
        if (Engine.isOn) {
            // do some work
        }
    }
    ...
}

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:

确定