Java 使用泛型实例变量创建单例类?
我有一个场景,其中有一个带有未来对象的bean(下面的类a)。我有另一个类(下面的类B),它是一个单例类,HashMap作为类型的实例变量,并实现了一个接口(下面的TestInterface),该接口再次实现了Callable 根据当前场景,用户只能传递对象类型的Future,但根据我的新要求,我希望对象类型为Generic。我已经修改了代码,这似乎是工作,但有很多警告,我甚至不知道我的更改是否正确。我确信有些情况下代码会失败。我面临的主要问题是初始化singleton类中GenericType的HashMap。有人能帮我吗 下面的代码是现有代码的示例 接口测试接口:Java 使用泛型实例变量创建单例类?,java,generics,Java,Generics,我有一个场景,其中有一个带有未来对象的bean(下面的类a)。我有另一个类(下面的类B),它是一个单例类,HashMap作为类型的实例变量,并实现了一个接口(下面的TestInterface),该接口再次实现了Callable 根据当前场景,用户只能传递对象类型的Future,但根据我的新要求,我希望对象类型为Generic。我已经修改了代码,这似乎是工作,但有很多警告,我甚至不知道我的更改是否正确。我确信有些情况下代码会失败。我面临的主要问题是初始化singleton类中GenericType
interface TestInterface extends Callable<Void>{
void doSomething(Future<Object> future, String id);
}
interface TestInterface extends Callable<Void>{
<T> void doSomething(Future<T> future, String id);
}
接口测试接口扩展可调用{
void doSomething(未来,字符串id);
}
A类
class A{
private Future<Object> future;
private CustomInteraface a;
public A(Future<Object> future, CustomInteraface a){
//do init
}
//getters and setters
}
class A<T>{
private Future<T> future;
private CustomInteraface a;
public A(Future<T> future, CustomInteraface a){
//do init
}
//getters and setters
}
A类{
私人未来;
私人银行面对a;
公共A(未来,客户A){
//开始
}
//接球手和接球手
}
B类
Class B implements TestInterface{
private HashMap<String, A> map = new HashMap();
private static B monitor = new B();
public Void call(){
HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
A a = (A) pair.getValue();
Future<Object> future = a.getFuture();
// Do something
}
public void doSomething(Future<Object> future, String id){
if(map.contains(id)){
//Do something
}
else{
A a = new A(future, null);
map.put();
}
}
}
Class B implements TestInterface{
private HashMap<String, A> map = new HashMap();
private static B monitor = new B();
public Void call(){
HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
A a = (A) pair.getValue();
//Code will definitely fail here as I'm trying to cast a future object of generic type to Object class
Future<Object> future = a.getFuture();
// Do something
}
public void doSomething(Future<T> future, String id){
if(map.contains(id)){
//Do something
}
else{
A<T> a = new A<T>(future, null);
map.put();
}
}
}
类B实现了TestInterface{
private HashMap map=new HashMap();
专用静态B监视器=新B();
公开作废通知(){
条目对=(HashMap.Entry)it.next();
A=(A)pair.getValue();
Future=a.getFuture();
//做点什么
}
公共void doSomething(未来,字符串id){
if(映射包含(id)){
//做点什么
}
否则{
A=新A(未来,空);
map.put();
}
}
}
我为Genrics所做的更改
接口测试接口:
interface TestInterface extends Callable<Void>{
void doSomething(Future<Object> future, String id);
}
interface TestInterface extends Callable<Void>{
<T> void doSomething(Future<T> future, String id);
}
接口测试接口扩展可调用{
void doSomething(未来,字符串id);
}
A类
class A{
private Future<Object> future;
private CustomInteraface a;
public A(Future<Object> future, CustomInteraface a){
//do init
}
//getters and setters
}
class A<T>{
private Future<T> future;
private CustomInteraface a;
public A(Future<T> future, CustomInteraface a){
//do init
}
//getters and setters
}
A类{
私人未来;
私人银行面对a;
公共A(未来,客户A){
//开始
}
//接球手和接球手
}
B类
Class B implements TestInterface{
private HashMap<String, A> map = new HashMap();
private static B monitor = new B();
public Void call(){
HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
A a = (A) pair.getValue();
Future<Object> future = a.getFuture();
// Do something
}
public void doSomething(Future<Object> future, String id){
if(map.contains(id)){
//Do something
}
else{
A a = new A(future, null);
map.put();
}
}
}
Class B implements TestInterface{
private HashMap<String, A> map = new HashMap();
private static B monitor = new B();
public Void call(){
HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
A a = (A) pair.getValue();
//Code will definitely fail here as I'm trying to cast a future object of generic type to Object class
Future<Object> future = a.getFuture();
// Do something
}
public void doSomething(Future<T> future, String id){
if(map.contains(id)){
//Do something
}
else{
A<T> a = new A<T>(future, null);
map.put();
}
}
}
类B实现了TestInterface{
private HashMap map=new HashMap();
专用静态B监视器=新B();
公开作废通知(){
条目对=(HashMap.Entry)it.next();
A=(A)pair.getValue();
//这里的代码肯定会失败,因为我试图将未来的泛型对象强制转换为对象类
Future=a.getFuture();
//做点什么
}
公共void doSomething(未来,字符串id){
if(映射包含(id)){
//做点什么
}
否则{
A=新A(未来,空);
map.put();
}
}
}
如果您可能将异构类型的
放入映射中,则需要使用通配符声明映射:
private HashMap<String, A<?>> map = new HashMap();
如果是singleton,那么“泛型”是什么意思?实际上,singleton类的一个实例变量是泛型的,在我的例子中是HashMap的值。我不知道如何初始化它,因为我的类是singelton,在创建类的过程中我正在实例化HashMap。@utkarsh31否,使用A
,如我的回答所示。