Skip to main content

Emmm,This problem is hard to describe.so 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){ } } 
//this class use as parent public abstract class Test { //there is generic function public <T extend Test> function(List<T> param){ } } 

If thea class is derived from 'Test', and I call function(List) on it, then the param type can be any of Test's child. but i want paramchildren and is not fixed to be the childchild's class objI'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(); } } 
 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 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

Emmm,This problem is hard to describe.so code like

//this class use as parent public abstract class Test { //there is generic function public <T extend Test> function(List<T> param){ } } 

If the class derived from 'Test', and call function(List), param type can be any Test's child. but i want param is the child class obj.

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

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

Source Link
Blate
  • 5
  • 1
  • 4

How to specify the child-class type in a generic method(in parent-class )

Emmm,This problem is hard to describe.so code like

//this class use as parent public abstract class Test { //there is generic function public <T extend Test> function(List<T> param){ } } 

If the class derived from 'Test', and call function(List), param type can be any Test's child. but i want param is the child class obj.

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