Emmm,This problem is hard to describe. My code is like:
//this class use as parent public abstract class Test { //there is generic function public <T extend Test> function(List<T> param){ } } If a class is derived from 'Test' and I call function(List) on it, then the param type can be any of Test's children and is not fixed to be the child's class I'm calling the method on, which actually is what I want to achieve.
A feasible solution is:
public <T extend Test> function(Class<T> clazz,List<T> param){ //check 'this' and clazz,if 'this' class not equal clszz,throw Exception if (this.getClass().equals(clazz)){ throw new Exception(); } } But is there a better solution? I think this problem can be solved during compilation instead of runtime. Sorry, my english is not good, But I want to know how to solve it.thanks
java //params List<Test> testList; List<Test1> test1List; List<Test2> test2List; Test1 test = new Test1(); / ** * it's okay for params is testList * it's okay for params is test2List * I expect param can only be test1List * / test1.function(test1List);java public <T extend this.class> function(List<T> param){ }