简体   繁体   English

Java中乘法有界类型参数的用途是什么?

[英]What are uses for multiply bounded type parameters in Java?

In Java generics you can use "&" to specify multiple interfaces as type bounds for a type parameter. 在Java泛型中,可以使用“&”将多个接口指定为类型参数的类型边界。 This allows us for example to manipulate objects of different types with common interfaces to be manipulated uniformly even if there is not an parent interface for those common ones. 例如,这允许我们使用通用接口来操纵不同类型的对象,以统一操纵这些接口,即使这些通用接口没有父接口也是如此。 My question is, how can this be used? 我的问题是,如何使用? For What purposes? 出于什么目的? I can imagine using this feature for collections, but how is it really than creating a new interface? 我可以想象使用此功能进行收藏,但是与创建新界面相比,它到底有什么用呢? This cannot be used dynamically, nor for type parameters. 这不能动态使用,也不能用于类型参数。 Is it just a syntactic sugar, or is there real use case for this feature? 仅仅是语法糖,还是此功能有实际用例?

Here is an example taken from my Java Generics book: 这是取自我的Java Generics书的示例:

public static <S extends Readable & Closeable, 
              T extends Appendable & Closeable> 
       void copy(S source , T target, int size){

    //code to copy from source to target 

}

The method above takes any source that implements both Readable and Closeable and any target that implements both Appendable and Closeable and copies from source to target. 上面的方法采用实现了ReadableCloseable以及实现AppendableCloseable任何目标,并从源复制到目标。 You might wonder why we can't simplify it to: 您可能想知道为什么我们不能将其简化为:

public static void copy(Reader source, Writer target, int size)

This will indeed admit most of the same classes but not all of them. 实际上,这将允许大多数相同的类,但不是全部。 For instance, PrintStream implements both Appendable and Closeable , but is not a subclass of Writer . 例如, PrintStream实现AppendableCloseable ,但它不是Writer的子类。 Furthermore, you can't rule out the possibility that some programmer using your code might have his or her own custom class that, say, implements Readable and Closeable but is not a subclass of Reader . 此外,您不能排除某些使用您的代码的程序员可能拥有他或她自己的自定义类的可能性,例如,该类实现ReadableCloseable但不是Reader的子类。

There are use cases for this, but there are generally alternate ways to achieve the same results. 有一些用例,但是通常有其他方法可以达到相同的结果。 This is just another tool in the toolbox available for Java developers. 这只是Java开发人员可以使用的工具箱中的另一个工具。

But let's say you have a JPanel that takes a custom JComponent that implements MyInterface . 但是,假设你有一个JPanel ,需要一个自定义JComponent实现MyInterface It can be one of several components, depending on another option selected on the panel. 它可以是多个组件之一,具体取决于面板上选择的另一个选项。 You want to reference this as a JComponent so that you can interact with it and place it, yet you also need to reference it as MyInterface to call some custom methods. 您希望将其引用为JComponent以便可以与之交互并放置它,但是还需要将其引用为MyInterface来调用一些自定义方法。

In this case, you cannot simply add the JComponent methods you need to MyInterface , because you have to call JPanel.add(component to put it on the overall panel. You can't make your interface somehow extend JComponent because Java doesn't work like that. You cannot create a custom extentsion to JComponent , because perhaps sometimes your object is another panel, sometimes it's a text field, and you don't want to restrict yourself. So you would reference it as a JComponent & MyInterface . 在这种情况下,您不能简单地将所需的JComponent方法添加到MyInterface ,因为必须调用JPanel.add(component将其放在整个面板上。您不能使您的接口以某种方式扩展JComponent因为Java无法正常工作那样,您无法为JComponent创建自定义扩展,因为有时您的对象是另一个面板,有时是一个文本字段,并且您不想限制自己,因此可以将其引用为JComponent & MyInterface

I was thinking, that an interresting use case might be using it in combination with multiple inheritance (composition and delegation), especially with generalization sets to create stronger constraints. 我当时在想,一个有趣的用例可能是将它与多重继承(组成和委托)结合使用,尤其是与泛化集一起创建更强的约束时。 For example in use case modeling, an actor might be an organization, system or subsystem, but at the same time also primary, secondary and ternary actor. 例如,在用例建模中,参与者可以是组织,系统或子系统,但同时也可以是主要,次要和三元参与者。 All of these might require different behaviour (eg ternary actor has interrests, but primary has goals), so they cannot be collapsed. 所有这些都可能需要不同的行为(例如,三元演员有中风,但主要演员有目标),因此它们不能崩溃。 Now to work with a bank, which might be Ternary actor as well as organization, you would use this feature. 现在要与可能是三元参与者和组织的银行合作,您将使用此功能。

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

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