Can generic types be extended to multiple classes in Java?
•
Java
I'm trying to write a method using generic parameters in Java. I want to limit it to one of three possible classes (field, method or constructor)
I tried the following title:
private static <T extends Field,Method,Constructor> T[] sort(T[] anArray)
In this way, however, it ignores generic parameters of type method or constructor Errors can also occur using the following:
private static <T extends Field | Method | Constructor> T[] sort(T[] anArray)
Is it possible to do such a thing in Java?
Solution
For your particular case (if you're talking about reflecting a particular class), you're lucky
You can use the accessibleobject course
private static <T extends AccessibleObject> T[] sort(T[] anArray)
You may also be interested in the member interface, depending on your requirements
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
二维码