简体   繁体   English

静态最终成员变量与get方法

[英]Static final member variables vs. get methods

I have a class with all the configuration from a property file. 我有一个包含所有来自属性文件的配置的类。

My first solution is this: 我的第一个解决方案是:

public class Config {

    public static final int disc;

    static {

        // Read property file and set properties

        disc = 5;
    }
}

Reading the information in this way: 以这种方式读取信息:

System.out.println(Config.disc);

The second solution is: 第二种解决方案是:

public class Config {

    private int disc;

    public void Config() {

        // Read property file and set properties

        disc = 5;
    }

    public int getDisc() {
        return this.disc;
    }
}

Reading in that way: 以这种方式阅读:

System.out.println(new Config().getDisc());

What's the best way and why? 最好的方法是什么,为什么? What the advantages and disadvantages? 有什么优缺点?

The answer depends on the meaning of disc : 答案取决于disc的含义:

  • If disc represents a constant, essentially giving a name to a numeric value, than a public final field is better 如果disc代表一个常数,本质上是给数值起一个名字,那么比public final字段更好
  • If disc represents a value that could change as the result of user's actions (ie if it is part of configuration) then a private variable with a getter is preferable. 如果disc表示的值可能因用户的操作而改变(即,如果它是配置的一部分),则首选带有getter的私有变量。

The second approach gives you more flexibility, should you decide to refactor your class in the future: it lets you initialize the private disc at a later time, or replace it with some other way of obtaining a value, eg by computing it from other values, or reading from another object. 第二种方法为您提供了更大的灵活性,如果您将来决定重构您的类:它可以让您在以后的时间初始化私有disc ,或用其他获取值的方式替换它,例如通过从其他值计算得出,或从另一个对象读取。

using getDisc() you can have one disc variable per object while static disc is shared across all the instances. 使用getDisc() ,每个对象可以有一个光盘变量,而所有实例之间都共享static disc

  • If you have different disc value for every instance of class, then go for second approach. 如果您对类的每个实例具有不同的disc值,则请采用第二种方法。
  • If you have same value which needs to be shared across all the instances of the class, then use the first approach 如果您具有需要在类的所有实例之间共享的相同值,请使用第一种方法

By they way, you cannot do System.out.println(Config.getDisc()); 通过它们,您将无法执行System.out.println(Config.getDisc()); . You can't call a non-static method using class name 您不能使用类名调用非静态方法

What about: public static final int DISC = 5; 那怎么办: public static final int DISC = 5; Then you can use Config.DISC to access your config value. 然后,您可以使用Config.DISC访问您的配置值。

System.out.println(Config.getDisc());

This is wrong. 错了 You can't call a non-static method with class name. 您不能使用类名称调用非静态方法。 You must create an object as following: 您必须创建一个对象,如下所示:

Config cfg = new Config();
System.out.println(cfg.getDisc());

Now, In your first case all the instances will share same copy of disc . 现在,在您的第一种情况下,所有实例将共享disc相同副本。 so, if it's not a constant, go for 2nd case. 因此,如果不是常数,请选择第二种情况。

Software Engineering Principles requires " Information Hiding ". 软件工程原理要求“ 信息隐藏 ”。 In your first solution your "disc" property is public and this property can be used in anywhere and it breaks "information hiding" principle. 在您的第一个解决方案中,“ disc”属性是公共的,并且该属性可以在任何地方使用,并且违反了“信息隐藏”的原则。 I vote for your second solution and recommend you to make "disc" private. 我投票赞成您的第二个解决方案,并建议您将“光盘”设为私有。

What you want in this case is really 在这种情况下,您真正​​想要的是

public static final int DISC = 5;

System.out.println(Config.DISC);

(Note that members like these are conventionally always written in capticals). (请注意,像这样的成员通常总是用英文写成)。


Your first solution has an error in it. 您的第一个解决方案有一个错误。 It won't compile because you try to set a final field multiple times. 它不会编译,因为您尝试多次设置一个final字段。 Each time you create a new Config() , you will assign a value to disc , but since it is final that won't work. 每次创建new Config() ,都将为disc分配一个值,但是由于它是最终值,因此无法正常工作。 What you probably wanted to suggest was to put it in the static constructor: 您可能想要建议的是将其放入静态构造函数中:

public class Config
{
    public static final int DISC;
    static
    {
        DISC = 5;
    }
}

However, I do not recommend it, because is is totaly find to do it the first way I wrote and is much easier as well. 但是,我不建议这样做,因为完全可以找到我写的第一个方法,而且也容易得多。

Your second solution has an error in it. 您的第二个解决方案中有一个错误。 It won't compile because you can't access the non-static method from a static context. 它不会编译,因为您不能从静态上下文访问非静态方法。 This means that you first will have to create an instance of Config, before you can call the getDisc() method. 这意味着您必须先创建Config的实例,然后才能调用getDisc()方法。 The better solution in this case is to declare the method as static. 在这种情况下,更好的解决方案是将方法声明为静态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM