我有一个非常简单的问题但到目前为止找不到任何东西。
我正在尝试创建两个类构造函数 第一个构造函数获取2个字符串和一个HashMap并初始化类变量。
public Foo(String a, String b, HashMap<String, String> c) {
this.a = a;
this.b = b;
this.c = c;
}
第二个构造函数应该只获取2个字符串并创建一个“默认”-HashMap。
通常您只需使用默认值调用this()
,但我找不到使用HashMap
执行此操作的方法。
public Foo(String a, String b) {
this(a, b, new HashMap<String, String>().put("x", "y").put("f","g"));
}
Eclipse标记错误:
类型不匹配:无法从
String
转换为HashMap<String,String>
否则this()
- 调用不能是函数中的第一个语句。
public Foo(String a, String b) {
HashMap<String, String> c = new HashMap<String, String>();
c.put("x", "y");
c.put("f", "g");
this(a, b, c);
}
任何想法如何解决这个问题?
最糟糕的情况我不得不复制代码,但我想知道是否有更好的方法。
答案 0 :(得分:5)
如果此Map是常量,您可以将其存储为常量并重用它。这样可以避免每次创建新Foo
时重新创建地图,但会在所有Foo
之间共享。
public class Foo {
private static final Map<String, String> DEFAULT = new HashMap<>();
static {
DEFAULT.put("x", "y");
DEFAULT.put("f","g");
}
public Foo(String a, String b) {
this(a, b, DEFAULT);
}
public Foo(String a, String b, Map<String, String> c) {
this.a = a;
this.b = b;
this.c = c;
}
}
您还可以创建一个返回正确值的静态方法。请注意,该方法必须是静态的,因为您无法在this()
内调用实例方法。
public class Foo {
public Foo(String a, String b) {
this(a, b, getDefaultMap());
}
public Foo(String a, String b, Map<String, String> c) {
this.a = a;
this.b = b;
this.c = c;
}
private static Map<String, String> getDefaultMap() {
Map<String, String> map = new HashMap<>();
map.put("x", "y");
map.put("f", "g");
return map;
}
}
答案 1 :(得分:4)
创建public class Room
{
public bool IsAvailable {get; set;}
public RoomType RoomType {get; set;}
public int RoomNo {get; set;}
public int Floor {get; set;}
public string RoomName {get; set;}
}
public enum RoomType
{
Single,
Double,
Twin,
King,
HoneymoonSuite
}
public class RoomManager
{
public List<Room> AllRooms {get; set;}
public RoomManager()
{
AllRooms = new List<Room>();
AllRooms.Add(new Room(){ RoomType=RoomType.Single,
RoomNo=1,
Floor=1,
RoomName="A101",
IsAvailable=true});
AllRooms.Add(new Room(){ RoomType=RoomType.Double,
RoomNo=2,
Floor=1,
RoomName="A102",
IsAvailable=false});
AllRooms.Add(new Room(){ RoomType=RoomType.HoneyMoonSuite,
RoomNo=1,
Floor=2,
RoomName="A201",
IsAvailable=true});
}
public bool IsAvailable(int roomNo)
{
//You need to check if roomNo is a valid RoomNo
return AllRooms.Any(r=>r.RoomNo==roomNo && r.IsAvailable);
}
public bool IsAvailable(string roomName)
{
//You need to check if roomName is valid RoomName
return AllRooms.Any(r=>r.RoomName==roomName && r.IsAvailable);
}
}
。
你可以简单地这样称呼它。
c
由于你的其他构造函数public Foo(String a, String b) {
this(a, b, new HashMap<String, String>());
c.put("x", "y");
c.put("f", "g");
}
将在this(String, String, HashMap<String, String>())
上分配新的HashMap
到c
调用方法也将填充此新创建的c
,并将分配您为其提供的默认值。
另一种解决方案可能是创建一个静态方法,然后调用它
HashMap
答案 2 :(得分:1)
public class SingleRoom
{
public SingleRoom
: base()
{
//Redefine the values of the array.
av = { true, true, false };
}
}
方法HashMap
返回.put
,因此在2参数构造函数中调用3参数构造函数时,您将传递String
。
答案 3 :(得分:0)
你可以这样做:
public Foo(String a, String b) {
this(a, b, new HashMap<String, String>(){
{
put("x", "y");
put("f", "g");
}
});
}
它创建了一个继承HashMap的匿名类,并使用put值定义了init块。
答案 4 :(得分:0)
你可以解决&#34;对此()的调用必须是第一个语句&#34;通过添加创建默认Map
实例的静态方法:
public Foo(String a, String b) {
this(a, b, createDefaultMap());
}
private static Map<String, String> createDefaultMap() {
Map<String, String> defaultMap = new HashMap<String, String>();
defaultMap.put("x", "y");
defaultMap.put("f", "g");
return defaultMap;
}
createDefaultMap()
必须是静态的,因为此时您可能无法访问半构造的实例。