csj*_*csj 42 java constructor final initialization
我有一个最终的非静态成员:
private final HashMap<String,String> myMap;
Run Code Online (Sandbox Code Playgroud)
我想使用构造函数调用的方法初始化它.由于myMap是final,我的"helper"方法无法直接初始化它.我当然有选择:
我可以直接在构造函数中实现myMap初始化代码.
MyConstructor (String someThingNecessary)
{
myMap = new HashMap<String,String>();
myMap.put("blah","blahblah");
// etc...
// other initialization stuff unrelated to myMap
}
Run Code Online (Sandbox Code Playgroud)
我可以让我的帮助器方法构建HashMap,将其返回到构造函数,然后让构造函数将对象分配给myMap.
MyConstructor (String someThingNecessary)
{
myMap = InitializeMyMap(someThingNecessary);
// other initialization stuff unrelated to myMap
}
private HashMap<String,String> InitializeMyMap(String someThingNecessary)
{
HashMap<String,String> initializedMap = new HashMap<String,String>();
initializedMap.put("blah","blahblah");
// etc...
return initializedMap;
}
Run Code Online (Sandbox Code Playgroud)
方法#2很好,但是,我想知道是否有某种方法可以让helper方法直接操作myMap.也许一个修饰符指示它只能由构造函数调用?
MyConstructor (String someThingNecessary)
{
InitializeMyMap(someThingNecessary);
// other initialization stuff unrelated to myMap
}
// helper doesn't work since it can't modify a final member
private void InitializeMyMap(String someThingNecessary)
{
myMap = new HashMap<String,String>();
myMap.put("blah","blahblah");
// etc...
}
Run Code Online (Sandbox Code Playgroud)
Yis*_*hai 15
方法#2是您的最佳选择.问题是,如果你在私有方法中有一个赋值,那么在构造函数调用它之外的类中没有任何东西可以阻止其他代码,这会产生第二个赋值给最终字段的问题.
Java没有单独的方法的构造,只能在构造期间调用.
为了完整起见,我们可以创建第三个选项,在初始化时分配映射,然后使用辅助方法填充它:
private final HashMap<String, String> myMap = new HashMap<String, String();
Run Code Online (Sandbox Code Playgroud)
然后:
MyConstructor (String someThingNecessary)
{
initializeMyMap(someThingNecessary);
// other initialization stuff unrelated to myMap
}
// helper doesn't work since it can't modify a final member
private void initializeMyMap(String someThingNecessary)
{
myMap.clear();
myMap.put("blah","blahblah");
// etc...
}
Run Code Online (Sandbox Code Playgroud)
如果你真的想要混淆,你可以使用初始化器而不是构造函数,但你不应该这样做,所以除非你真的需要知道,否则我不会扩展它.
cje*_*nek 13
如何实现初始化HashMap的私有构造函数,然后让主构造函数调用该私有构造函数?
例如 -
// Helper function to initialize final HashMap.
private MyConstructor()
{
myMap = new HashMap<String,String>();
myMap.put("blah","blah");
}
MyConstructor (String someThingNecessary)
{
// Initialize the HashMap.
this();
// Other initialization code can follow.
}
Run Code Online (Sandbox Code Playgroud)
您可以根据需要修改私有帮助程序构造函数的签名(例如,提供参数数据或使签名与任何公共构造函数不同).