我想创建一个包含泛型类字段的Singleton。
并为这些字段使用getter和setter来接收类型。我怎么能这样做?
public class User<U,O> {
private static final User instance;
private U userId;
private O organization;
private User() {
}
public static synchronized User getInstance() {
if(instance == null){User = new User();}
return instance;
}
//getter and setter for U and O
}
如何在没有施法需要的情况下“按类型”调用Singleton之外的getter和setter?
答案 0 :(得分:-1)
我认为您只需要输入getInstance()
:
尝试:
public class User<U,O> {
private static final User<> instance;
private U userId;
private O organization;
private User() {
}
public static <A, B> synchronized User<A, B> getInstance() {
if (instance == null) {
instance = new User<>();
}
return instance;
}
//getter and setter for U and O
}