Home > Back-end >  About the generic class <T> as a parameter to the method call
About the generic class <T> as a parameter to the method call

Time:10-10

There is a class
Classs AAA {
int i;
T T;
}
And there is a User class
The class User {
String name;
}

Methods
The public & lt; T> T Fun (Class T1) {
Return the t1;
}
Now want to ask how the parameters AAA To the Fun inside
call like this: class1 & lt; User> C=Fun (AAA .class) so that I can't,,,
Want to ask, how should call,,,

CodePudding user response:

Who knew that raise a point ah,

CodePudding user response:

You the Fun is the wrong way you write go in and see you return in the compiler is a Java. Lang, Class and all that is needed is a normal Class, so you this method is wrong the compiler complains

CodePudding user response:

Brother, you ask the question, accurate description: multilayer nested class how to obtain a generic class?

Error:
1, AAA .class
2, the new AAA GetClass () ()

The above two kinds of not sure, the first compilation is not through, the second external class resolution through, internal class will be resolved as a map

Common solutions: with the help of the fastJson TypeReference
The results first converted to a String, use String directly. The class
Then
AAA Data=https://bbs.csdn.net/topics/JSON.parseObject (jsonStr, new TypeReference () {
}. GetType ());

Done!
Prerequisites: the result is the json format to convert multilayer nested generic class
  • Related