gpt4 book ai didi

java - 如何通过 Java 中泛型的特定派生来专门化对泛型函数的调用?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:27:27 24 4
gpt4 key购买 nike

我有一个 Base 类和它的几个派生子类,名为 ChildN

我还有一个 Container 类,带有 ChildN 特定的方法。

最后我有一个主类,我希望将所有 Base-generic 逻辑移动到 helper 方法中,但是在这个逻辑中间的某个地方我可能需要放一个 ChildN -特定对象到 Container 类的正确方法中。我怎么做?如何使用正确的 ChildN 类型专门化对 hassleSafeAdder 的调用,以便可以在此方法中访问它并且我可以对其进行转换,或者做一些其他事情来正确调度反对正确的方法?

// 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());

最佳答案

您可以使用 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);
}
}
}

关于java - 如何通过 Java 中泛型的特定派生来专门化对泛型函数的调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com