有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

泛型Java:如何为实例化包装类的抽象类的子类创建包装类

我有一个接口(我们称之为FooInterface),我在抽象类(我们称之为Bar)的子类(我们称之为A、B、C)的不同子类(我们可以称之为X、Y、Z)中实现它。但是,在所有方法中编写(复制)相同的方法感觉很浪费,考虑到它们只不过是它们从接口扩展的子类+这些方法,更是如此

因此,我决定删除X、Y、Z层,并使FooInterface成为实际的类Foo包装条实例。但我似乎想不出一个好方法来实现这一点,我的第一个想法是一个通用包装器类:

public abstract class Bar {/*code*/}

public class A extends Bar {/*code*/}
public class B extends Bar {/*code*/}
public class C extends Bar {/*code*/}

/*
 * Removed thanks to using the nifty wrapper Foo now!
 * I am so happy I decided to do this,
 *  can't imagine the horrors I would dream of at night if I had
 *  an extra redundant class for each subclass of Bar like this!
 * public class X extends A implements FooInterface {/*implementing FooInterface*/}
 * public class Y extends B implements FooInterface {/*implementing FooInterface*/}
 * public class Z extends C implements FooInterface {/*implementing FooInterface*/}
*/

public class Foo<T extends Bar> {
    private T t;
    public Foo() {t = /*code instantiating T*/}
    //code implementing the methods from FooInterface
}

然而,这是行不通的,因为在java中使用泛型类型时,似乎很难获得t的实例

我考虑过做一些类似于让T成为枚举的事情:

public class Foo<T extends BarType> {
    public enum BarType { A, B, C; }
    private T t;
    public Foo() {
        switch(T) {
            // code instantiating t by using an ugly hard-coded switch-case...
            // please don't make me!
        }

        //code implementing the methods from FooInterface
    }
}

但我真的无法想象一个人怎么能使用这样的枚举。因此,在这一点上,我正在认真考虑放弃空的构造函数,并按照以下思路做一些事情:

// No longer generic and instead pass type to constructor!
public class Foo {
    public enum BarType { A, B, C; }
    private T t;
    public Foo(Bartype bartype) {
        switch(T) {
            // code instantiating t by using an ugly hard-coded switch-case...
            // please don't make me!
        }

        //code implementing the methods from FooInterface
    }
}

虽然为了避免使用硬编码的switch语句,我更倾向于这个版本:

public class Foo<T extends Bar> {
    private T t;
    // Foo is used as such: 'Foo<T> foo = new Foo<T>(T.class);'
    public Foo(Class<T> clazz) throws InstantiationException,
            IllegalAccessException {
        // why is it customary to spell this with z?
        t = clazz.newInstance()

        //code implementing the methods from FooInterface
    }
}

如何在java中正确地制作这样的包装器?我也考虑过其他方法,比如在泛型类中,将工厂传递给构造函数,或者执行参数化类型操作,或者在非泛型类中,直接将正确类的实例传递给构造函数。但所有这些似乎都是一个大麻烦,而且非常丑陋,我甚至更喜欢使用像这样的枚举的硬编码开关案例

编辑: 有人问我为什么不让Bar实现FooInterface,这是因为Bar是一个很好的纯数据结构,FooInterface的作用是让它拥有一些对更专业的实现有用的方法。例如,假设Bar等是普通列表(因此可能我甚至不是实现它们的人),但我的实现还必须跟踪哪些列表很酷:

public abstract class List; // Bar
public class LinkedList extends List; // A
public class ArrayList extends List // B
public interface CoolLists { // FooInterface
    boolean isCool();
    void setCool(boolean cool);
}
public class CoolLinkedList extends LinkedList implements CoolList; // X
public class CoolArrayList extends ArrayList implements CoolList; // Y

不,我并没有真正重新实现列表,如果我想跟踪“酷列表”,我不会在其中存储布尔值,而是使用类似于存储“酷”列表的哈希集:p


共 (1) 个答案

  1. # 1 楼答案

    不应该传递Class<T>,而应该将Supplier<T>传递给Foo的构造函数。要获得一个实例,只需调用t = supplier.get();。这可以避免您必须处理反射问题。要获得供应商,您可以使用A::newB::newC::new。要创建Foo的实例,可以调用:

    new Foo<A>(A::new);
    

    不涉及反射和异常处理

    另见this答案