简体   繁体   English

Java中用于内部类的泛型类型

[英]Generic type in java for inner classes

I have abstract class with a generic type parameter, but it seems that the inner class can not access this type: 我有一个带有泛型类型参数的抽象类,但是内部类似乎无法访问此类型:

public abstract class DataProcessor<T> {

    protected interface CallBack {
        List<T> parse(String response) throws ServerException, JSONException;// error
    }
}

What's the problem? 有什么问题?


update: 更新:

Usage: 用法:

public class XProcessor extend DataProcessor<X>{
}
public class YProcessor extend DataProcessor<Y>{
}

XProcessor xp=new XProcessor();
YProcessor yp=new YProcessor();

xp.setCallback(new DataProcessor.CallBack<X>(){
    ....
});

But I thought that since the XProcessor have defined the X in the class defined, so I wonder if I can ignore the generic parameter like this: 但是我认为既然XProcessor在定义的类中定义了X ,所以我想知道是否可以忽略这样的泛型参数:

xp.setCallback(new XProcessor.CallBack(){ //without specify the generic type 
    .....
});

It is not possible for you are intending. 您的打算是不可能的。 To avoid compilation error add the type parameter to Callback interface. 为避免编译错误,将type参数添加到Callback接口。

public abstract class DataProcessor<T> {

    protected interface CallBack<T> {
        List<T> parse(String response) throws ServerException, JSONException;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM