简体   繁体   English

Java泛型继承

[英]Java Generics Inheritance

I have 2 different types of data managers, one inherits the other and both rely on 1 base data class and looks like this: 我有2种不同类型的数据管理器,一种继承另一种,都依赖于1个基本数据类,如下所示:

public abstract class BaseDataType {
    ...
}

public class BaseManager<DataType extends BaseDataType> implements ListModel{
    ...
    public final DataType get(index i){
        return dataList.get(i);
    }
    ...
}

public class BaseSQLManager<DataType extends BaseDataType, Adapter extends SQLAdapter>
    extends BaseManager<DataType>{
    ...
}

My understanding that using BaseSQLManager m =new BaseSQLManager<Property, PropertSQLAdapter>(); 我的理解是使用BaseSQLManager m =new BaseSQLManager<Property, PropertSQLAdapter>(); would allow me to use Property p = m.get(i); 允许我使用Property p = m.get(i); however I am informed that the get method returns BaseDataType. 但是我被告知get方法返回BaseDataType。

I'm sure I'm missing something small here and I can't figure it out. 我确定我在这里缺少一些小东西,我无法弄明白。 An explanation of why this doesn't work would really help. 解释为什么这不起作用真的会有所帮助。 Thanks 谢谢

BaseSQLManager m =new BaseSQLManager<Property, PropertSQLAdapter>();

Yes, you are missing the type parameter in the variable declaration, ie the type of m does not constrain the type parameter, which is why get() is only known to return something that extends BaseDataType. 是的,你在变量声明中缺少type参数,即m的类型不约束type参数,这就是为什么get()只知道返回扩展BaseDataType的东西。 It should be: 它应该是:

BaseSQLManager<Property, PropertSQLAdapter> m 
    = new BaseSQLManager<Property, PropertSQLAdapter>();

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

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