Java 如何使用构造函数创建新地图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3271320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a new map with constructor?
提问by user386537
I am wanting to do two things
我想做两件事
Create a private instance variable which is a map
To create an empty instance in my constructor that impliments a map and assigns it to the previous private instance variable.
创建一个私有实例变量,它是一个映射
在我的构造函数中创建一个空实例来实现映射并将其分配给前一个私有实例变量。
The Private instance I have is
我拥有的私有实例是
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
but how would create an instance variable in the constructor that would reference the private variable thesaurus upon the constructors creation.
但是如何在构造函数中创建一个实例变量,在构造函数创建时引用私有变量同义词库。
For example
例如
public class Book{
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
public Book(){
super();
/* What do i put here as an empty instance
* variable that implements a map and how
* do i assign it to thesaurus?
*/
}
回答by polygenelubricants
It's not clear what you're asking, but here are some points:
不清楚你在问什么,但这里有一些要点:
- You can't declarean instance variable in a constructor; you have to do declare it as a member of the type (i.e. as a field).
- You can assign valuesto already declared instance variables in a constructor.
- You don't have toassign values to instance variables in a constructor; you can do it at the declarations.
- 不能在构造函数中声明实例变量;您必须将其声明为该类型的成员(即作为字段)。
- 您可以指定值在构造函数中已声明的实例变量。
- 您不必在构造函数中为实例变量赋值;你可以在声明中做到这一点。
When you write something like this:
当你写这样的东西时:
public class Book{
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
//...
}
Then you've declared thesaurus
to be an instance variable of class Book
, and you also initialized its value to be a new HashMap
. Since this field is final
, you can no longer set its value to be anything else (barring reflection-based attacks).
然后,您已声明thesaurus
为 的实例变量class Book
,并将其值初始化为new HashMap
。由于此字段是final
,您不能再将其值设置为其他任何值(除非基于反射的攻击)。
You can, should you wish to choose so, move the initialization into the constructor. You can do this even when the field is final
(subject to various definite assignment rules).
如果您愿意,您可以将初始化移动到构造函数中。即使字段是final
(受各种明确分配规则的约束),您也可以这样做。
public class Book{
private final Map<Character, SortedSet<String>> thesaurus;
public class Book {
thesaurus = new HashMap <Character, SortedSet<String>>();
}
//...
}
Something like this is done sometimes when e.g. the creation of the initial value may throw a checked exception, and therefore needs to be put in a try-catch
block.
有时会在例如创建初始值可能引发已检查的异常时执行这样的操作,因此需要将其放入try-catch
块中。
Another option is to initialize fields in an instance initializer block:
另一种选择是在实例初始化块中初始化字段:
private final Map<Character, SortedSet<String>> thesaurus;
{
thesaurus = new HashMap <Character, SortedSet<String>>();
}
And yet another option is to refactor said instance initializer block into a helper method:
还有一个选择是将所述实例初始化块重构为一个辅助方法:
private final Map<Character, SortedSet<String>> thesaurus = emptyMap();
private static Map<Character, Sorted<String>> emptyMap() {
return new HashMap <Character, SortedSet<String>>();
}
References
参考
Related questions
相关问题
- Initialize final variable before constructor in Java
- Proper way to declare and set a private final member variable from the constructor in Java?
- In Java, can a final field be initialized from a constructor helper?
- Java - Can final variables be initialized in static initialization block?
- Best Practice: Initialize class fields in constructor or at declaration?
回答by Matthew Flaschen
You are already initializing your thesaurus variable with a map. You can move it to the constructor, like:
您已经在使用地图初始化同义词库变量。您可以将其移动到构造函数,例如:
public class Book
{
private final Map<Character, SortedSet<String>> thesaurus;
public Book(){
this.thesaurus = new HashMap <Character, SortedSet<String>>();
}
}
There's no need to change, though. Either way, the instance field will be initialized every time an instance is created. Also note that you don't need super()
here since it's implicit.
不过没必要改。无论哪种方式,每次创建实例时都会初始化实例字段。另请注意,您不需要super()
此处,因为它是隐式的。