在Java中从构造函数声明和设置私有最终成员变量的正确方法? [英] Proper way to declare and set a private final member variable from the constructor in Java?

查看:205
本文介绍了在Java中从构造函数声明和设置私有最终成员变量的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多种方法可以从构造函数中设置成员变量。我实际上是在讨论如何正确设置最终成员变量,特别是一个由助手类加载条目的映射。

There are different ways to set a member variable from the constructor. I am actually debating how to properly set a final member variable, specifically a map which is loaded with entries by a helper class.

public class Base {
    private final Map<String, Command> availableCommands;
    public Base() {
        availableCommands = Helper.loadCommands();  
    }
}

在上面的示例中,帮助程序类如下所示:

In the above example the helper class looks like this:

public class Helper {
    public static Map<String, Command> loadCommands() {
        Map<String, Command> commands = new HashMap<String, Command>();
        commands.put("A", new CommandA());
        commands.put("B", new CommandB());
        commands.put("C", new CommandC());

        return commands;
    }
}

我的想法是,使用在构造函数中设置此类变量的方法。因此,基类如下所示:

My thought is, that is better practice to use a method to set such a variable in the constructor. So Base class would look something like this:

public class Base {
    private final Map<String, Command> availableCommands;
    public Base() {
        this.setCommands();  
    }
    private void setCommands() {
        this.availableCommands = Helper.loadCommands();
    }
}

但现在我无法维护最终修饰符并获得编译器错误(无法设置最终变量)

But now I cannot maintain the final modifier and get a compiler error (Final variable cannot be set)

另一种方法是:

public class Base {
    private final Map<String, Command> availableCommands = new HashMap<String, Command>();
    public Base() {
        this.setCommands();
    }
    private void setCommands() {
        Helper.loadCommands(availableCommands);
    }
}

但在这种情况下,Helper类中的方法将更改为:

But in this case the method in the Helper class would change to:

public static void loadCommands(Map<String, Command> commands) {
    commands.put("A", new CommandA());
    commands.put("B", new CommandB());
    commands.put("C", new CommandC());
}

所以区别在于我在哪里用 new HashMap< String,Command>(); ?我的主要问题是,鉴于部分功能来自此,是否存在推荐的方法

So the difference is where do I create a new map with new HashMap<String, Command>();? My main question is if there is a recommended way to do this, given that part of the functionality comes from this Helper's static method, as a way to load the actual map with entries?

我是在基类还是Helper类中创建新地图的? strong>在这两种情况下,Helper都将进行实际加载,而Base对包含具体命令的地图的引用将是私有的且是最终的。

Do I create the new map in my Base class or the Helper class? In both cases Helper will do the actual loading and Base's reference to the map holding the concrete commands will be private and final.

也许还有其他更优雅的方式

Are there perhaps other more elegant ways to do this besides the options I am considering?

推荐答案


  1. 如果要使它不变,您是否还能这样做?不需要使用第三方API,可以使用: java.util.Collections.unmodifiableMap(Map m)

最常见的方法是:




public class Base {
private final Map availableCommands;
public Base(){
  availableCommands=new HashMap(); // or any other kind of map that you wish to load
  availableCommands = Helper.loadCommands(availableCommands);  
 }
}

这篇关于在Java中从构造函数声明和设置私有最终成员变量的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆