简体   繁体   English

是否可以将泛型类型扩展为Java中的多个类?

[英]Is it possible to extend the generic type to multiple classes in Java?

I am trying to write a method in Java that uses a generic parameter and which I want to restrict to being exclusively either one of three possible classes (either Field , Method , or Constructor ). 我试图在Java中编写一个使用泛型参数的方法,我想将其限制为三个可能的类之一( FieldMethodConstructor )。

I've tried the following header: 我试过以下标题:

private static <T extends Field,Method,Constructor> T[] sort(T[] anArray)

But this way it ignores generic parameters of type Method or Constructor . 但是这样它会忽略MethodConstructor类型的泛型参数。 Using the following also produces an error: 使用以下内容也会产生错误:

private static <T extends Field | Method | Constructor> T[] sort(T[] anArray)

Is it possible to do such a thing in Java ? 是否有可能在Java做这样的事情?

For your particular case (If you are talking about the Reflection specific classes), you are in luck. 对于您的特定情况(如果您正在讨论反射特定类),您很幸运。

You can use the AccessibleObject class. 您可以使用AccessibleObject类。

private static <T extends AccessibleObject> T[] sort(T[] anArray)

You may also be interested in Member interface depending on your requirement. 您可能还对Member界面感兴趣,具体取决于您的要求。

Type-parameter bound cannot be defined using the or operator ( | ). 无法使用或运算符( | )定义类型参数绑定。

It's only possible to restrict a type-parameter T to extend a class and multiple interfaces, though, by using the & operator: 但是,只能通过使用&运算符来限制类型参数T以扩展类和多个接口:

<T extends SomeClass & FirstInterface & SecondInterface>

No, but you could use a common interface/super class, just use Object and check inside the method or provide separate overloads. 不,但您可以使用通用接口/超类,只需使用Object并检查方法内部或提供单独的重载。

Note that A & B would be possible, ie require that a generic type implements both types. 请注意, A & B是可能的,即要求泛型类型实现这两种类型。 You can't use or (ie A | B ) because the compiler can't/won't determine a common super type - if it even exists besides Object - and thus your implementation can't work on any of the types because when in doubt you'll get passed the wrong type. 你不能使用 (即A | B ),因为编译器不能/不会确定一个常见的超类型 - 如果它甚至存在于Object之外 - 因此你的实现不能在任何类型上工作,因为当有疑问你会被错误的类型通过。

Think about it: if you were allowed to declare T as T extends Field | Method 想一想:如果你被允许宣布TT extends Field | Method T extends Field | Method , how would you use it inside the code? T extends Field | Method ,你会如何在代码中使用它? All you'd really know is that both types provide the methods of Object . 你真正知道的是两种类型都提供了Object的方法。

Generics are meant to remove the need for (potentially failing) casts in order to access a specific type's methods (ie the generic type's upper bound). 泛型旨在消除(可能失败的)强制转换的需要,以便访问特定类型的方法(即泛型类型的上限)。 Restricting parameter types comes as a consequence of this but is not a primary objective the way you seem to want to use it. 限制参数类型就是这样的结果,但并不是您希望使用它的主要目标。 That's what method overloads are for. 这就是重载的方法。

Edit: I just read Codebender's answer and that's what I mentioned in my first sentence: use a common interface or, as is the case here, super class. 编辑:我刚刚阅读了Codebender的答案,这就是我在第一句话中提到的:使用一个通用界面,或者就像这里的情况一样,使用超级类。 That's probably the best way to go. 这可能是最好的方式。

Field , Constructor and Method each inherit from Member . FieldConstructorMethod均继承自Member You could just do: 你可以这样做:

private static <T extends Member> T[] sort(T[] anArray)

It wouldn't make sense to specify a type bound which could match one of several unrelated types. 指定一个可以匹配几个不相关类型之一的类型绑定是没有意义的。 The whole point of generics is to give the compiler enough information to determine what methods would be available to the type T given that we don't know exactly what T will actually be. 泛型的全部意义在于为编译器提供足够的信息来确定哪种方法可用于类型T因为我们不确切知道T究竟是什么。 If it can be one of three things, how can the compiler possibly know which methods should be allowed? 如果它可以是三件事之一,编译器怎么可能知道应该允许哪些方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM