具有泛型的返回类型

时间:2012-12-14 13:49:06

标签: java generics

我正在尝试使用以下逻辑:

public class Wrapper {
    public static class General {
        public void copy(General g) {
            // copy fields
        }
        // General contents.
    }

    public static class Specific extends General {
        // Specific contents.
    }

    public static class GeneralLogic<T extends General> {
        public T load(ResultSet rs) {
            General o = new General();
            // Do stuff
            return new General();
        }
    }

    public static class SpecificLogic<T extends Specific> extends GeneralLogic<T> {
        @Override
        public T load(ResultSet rs) {
            Specific o = new Specific();
            o.copy(super.load(rs));
            // Do stuff
            return new Specific();
        }
    }
}

两个返回行都会导致编译错误:Type mismatch: cannot convert from Wrapper.General to T(第二次返回时为Wrapper.Specific to T)。

4 个答案:

答案 0 :(得分:2)

如果TGeneral的子类型,则General的实例不一定是T。如果要返回T,,则必须从另一个工厂方法(作为参数或抽象方法)获取它,或者接受Class<? extends T>并通过反射实例化它。

答案 1 :(得分:0)

嗯,你不能确定T是一般的编译器抱怨。你应该做这样的事情:

   public  T  ResultSet (ResultSet rs){
        T t=getSomeObject();
        return t;

答案 2 :(得分:0)

当你说“T extends General”时,General是超类,T是子类

但是在返回类型中,您尝试返回Parent(General)对象,而返回类型expect是T(请记住,Child类引用不能保存父类对象)。

像这样更改你的部分代码。

public static class GeneralLogic<T extends General> {
        public General load(Object rs) {
            General o = new General();
            // Do stuff
            return new General();
        }
    }

    public static class SpecificLogic<T extends Specific> extends GeneralLogic<T> {
        @Override
        public Specific load(Object rs) {
            Specific o = new Specific();
            o.copy(super.load(rs));
            // Do stuff
            return new Specific();
        }
    }

但是如果让返回类型为T非常重要,那么你需要创建一个Adapter类,它将你的General(父)对象转换为child(T)然后返回它

答案 3 :(得分:0)

我会这样做

 public static class General {
    public void copy(General g) {
        // copy fields
    }
    // General contents.
}

public static class Specific extends General {
    // Specific contents.
}

public interface Logic<T extends General>  {
    public T load(ResultSet rs);
}

public static class GeneralLogic implements Logic<General> {

    @Override
    public General load(ResultSet rs) {
        return new General();
    }

}

public static class SpecificLogic extends GeneralLogic {

    @Override
    public General load(ResultSet rs) {
        Specific o = new Specific();
        o.copy(super.load(rs));
        // Do stuff
        return o;
    }
}