In Java, can I specify any amount of generic type parameters?

前端 未结 3 1386
野的像风
野的像风 2021-01-17 16:14

I am looking to create a particular type of interface in Java (although this is just as applicable to regular classes). This interface would need to contain some me

3条回答
  •  庸人自扰
    2021-01-17 16:40

    Given the context you provided I would recommend using a List as a parameter. If these parameters have something in common, you can restrain your list to instead of using List. If not, you may still want to use marker interface.

    Here is an example.

    public class Main {
    
        public static void main(String[] args) {
            delegate(asList(new ChildOne(1), new ChildTwo(5), new ChildOne(15)));
        }
    
        private static  void delegate(List list) {
            list.forEach(item -> {
                switch (item.type) {
                    case ONE: delegateOne((ChildOne) item); break;
                    case TWO: delegateTwo((ChildTwo) item); break;
                    default: throw new UnsupportedOperationException("Type not supported: " + item.type);
                }
            });
        }
    
        private static void delegateOne(ChildOne childOne) {
            System.out.println("child one: x=" + childOne.x);
        }
    
        private static void delegateTwo(ChildTwo childTwo) {
            System.out.println("child two: abc=" + childTwo.abc);
        }
    
    }
    
    public class Parent {
        public final Type type;
    
        public Parent(Type type) {
            this.type = type;
        }
    }
    
    public enum Type {
        ONE, TWO
    }
    
    public class ChildOne extends Parent {
        public final int x;
    
        public ChildOne(int x) {
            super(Type.ONE);
            this.x = x;
        }
    }
    
    public class ChildTwo extends Parent {
        public final int abc;
    
        public ChildTwo(int abc) {
            super(Type.TWO);
            this.abc = abc;
        }
    }
    

    The biggest flaw of this solution is that children have to specify their type via enum which should correspond to the casts in the switch statement, so whenever you change one of these two places, you will have to remember to change the other, because compiler will not tell you this. You will only find such mistake by running the code and executing specific branch so test driven development recommended.

    提交回复
    热议问题