How can I use this generic specifically in Java to specify calls to generic functions?
•
Java
I have a base class with few derived children named childn
I also have a container class with childn specific methods
Finally, I have a main class. I want to move all base generic logic to the helper method, but somewhere in the middle of this logic, I may need to put a childn specific object into the correct method of the container class What do I do? How to use the correct childn type to specifically call hasslesafeadder, so you can access it in this method and convert it, or do something else that can correctly schedule objects to the correct method?
// 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()); } }
Expected pseudocode:
hassleSafeAdder<Child1>(container,new Child1()); hassleSafeAdder<Child2>(container,new Child2()); hassleSafeAdder<Child3>(container,new Child3()); hassleSafeAdder<Child4>(container,new Child4());
Solution
You can use the double dispatch mode
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); } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码