Returns the type of a method using a private Generic class?

I am trying to write a generator of a generator method that will return various objects, as shown below:

GenricClass<Object1> genCls1 = getNewRequest(Object1.class);
GenricClass<Object2> genCls2 = getNewRequest(Object2.class);
GenricClass<Object3> genCls3 = getNewRequest(Object3.class);

I want this to getNewRequestreturn an object that contains a common class object.

What should be the signature of my method getNewRequest?

public `?` getNewRequest(Class classtype) {...}
+4
source share
3 answers

You can declare a generic parameter type in the class that contains the method getNewRequest, or directly in the method.

For example, declare a generic parameter Tin a method getNewRequest:

public <T> GenricClass<T> getNewRequest(Class<T> classtype)
+4
source

classType T GenericClass . :

public GenericClass<T> getNewRequest(T classtype) { 
}
0

- :

public <T> GenericClass<T> getNewRequest(Class<T> classtype) {

}
0

Source: https://habr.com/ru/post/1685653/


All Articles