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

标签 java generics

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

因此,我决定删除 X、Y、Z 层,并使 FooInterface 成为包装 Bar 实例的实际类 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 中正确制作这样的包装器?我也考虑过其他方法,例如在泛型类中将工厂传递给构造函数或执行 ParameterizedType 操作,或者在非泛型中直接将正确类的实例直接传递给构造函数。但所有这些看起来都是一个大麻烦,而且非常丑陋,我什至更喜欢带有这样的枚举的硬编码开关盒......

编辑: 正如有些人问我为什么不直接让 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

不,我实际上并没有重新实现列表,如果我想跟踪“酷列表”,我不会在其中存储 boolean 值,而是使用 HashSet 之类的东西存储“酷”的:P

最佳答案

而不是传递 Class<T> ,你应该传递 Supplier<T>Foo 的构造函数。要获取实例,只需调用 t = supplier.get(); 。这可以让您不必处理反射问题。要获得供应商,您可以使用 A::new , B::newC::new 。创建 Foo 的实例你会打电话:

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

不涉及反射,也不涉及异常处理。

另请参阅this回答。

关于Java:如何为抽象类的子类创建包装类来实例化包装类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41946348/

相关文章:

java - 返回 Ljava.lang.Object 的字符串值

java - 贪吃蛇游戏。如何让蛇吃烂苹果后失去一根尾部?

generics - 如果我为 B 实现 From<A>,是否也会为 Vec<B> 实现 From<Vec<A>>?

java - 使用泛型实现接口(interface)时返回错误

java - 如何使用 Guice 注入(inject) Google App Engine 数据存储?

java - 为什么在构造函数不同步的情况下final关键字可以保证安全发布?

java - 使用 Java 泛型引用当前类型

delphi - 字符串记录的 TDictionary 哈希被破坏

java - 通过地理编码获取建筑物的坐标

java - 如何制作具有通用类型和 Android Fragment 继承的接口(interface)?