如何在Java中使用该泛型的特定派生来专门调用泛型函数?

时间:2013-11-29 23:51:54

标签: java generics syntax polymorphism

我有一个Base类,很少有派生的孩子,名为ChildN

我还有一个Container课程,其中包含ChildN个特定方法。

最后我有一个主类,我希望将所有Base - 泛型逻辑移动到辅助方法中,但是在这个逻辑的中间某处我可能需要设置ChildN特定的将对象转换为Container类的正确方法。我怎么做?如何使用正确的hassleSafeAdder类型专门调用ChildN,以便在此方法中可以访问它并且我可以转换它,或者做一些其他能够正确地将对象调度到正确方法的东西?< / p>

// base class
public static class Base { }

// derived classes
public static class Child1 extends Base { }
public static class Child2 extends Base { }
public static class Child3 extends Base { }
public static class Child4 extends Base { }

// class with derived types in method signature
public static class Container {
    public void add(Child1 obj) { }
    public void add(Child2 obj) { }
    public void add(Child3 obj) { }
    public void add(Child4 obj) { }
}

// main class where execution happens
public static class Main {
    private static <T extends Base> void hassleSafeAdder(Container container, T value) {
        // do some hassle
        container.add(value); // error, because T could be any Base derivation, while container has only ChildN-specific ones.
        // cleanup hassle resources
    }

    public static void main(String[] args) {
        Container container = new Container();

        // how to parametrize this calls so they will be valid?
        hassleSafeAdder(container, new Child1());
        hassleSafeAdder(container, new Child2());
        hassleSafeAdder(container, new Child3());
        hassleSafeAdder(container, new Child4());
    }
}

所需的伪代码:

        hassleSafeAdder<Child1>(container, new Child1());
        hassleSafeAdder<Child2>(container, new Child2());
        hassleSafeAdder<Child3>(container, new Child3());
        hassleSafeAdder<Child4>(container, new Child4());

1 个答案:

答案 0 :(得分:7)

您可以使用Double Dispatch模式。

class Ideone
{
private static <T extends Base> void hassleSafeAdder(Container container, T value) {
        value.addToContainer(container);
    }

public static void main (String[] args) throws java.lang.Exception {
    Container container = new Container();

    hassleSafeAdder(container, new Child1());
    hassleSafeAdder(container, new Child2());
    hassleSafeAdder(container, new Child3());
    hassleSafeAdder(container, new Child4());

}

public static class Container {
    public void add(Child1 obj) { }
    public void add(Child2 obj) { }
    public void add(Child3 obj) { }
    public void add(Child4 obj) { }
}

public static abstract class Base { 

    public abstract void addToContainer(Container container);

}

public static class Child1 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}

public static class Child2 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}

public static class Child3 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}

public static class Child4 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}
}