Java – can class types be passed and used as return types?
•
Java
I have a method to use the parser. The example of parser call is:
SpecificClass ret = parser.parse(getOutputStream(),SpecificClass.class);
Note that the return type is the same as the return type specified as parameter
Now, I want to create a method to perform this call and return the specific class type I want For example:
public $$some construct I don't kNow$$invokeParser(Class<?> c){ //... operations.... return parser.parse(getOutputStream(),c); //c works,I can pass it }
Is it possible?
Solution
You can make the method generic Declare your type parameter with < T > and return t
public <T> T invokeParser(Class<T> c) {
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
二维码