本文介绍了在Java中指定多个接口的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有两个接口, HasClickHandlers DoesFancyFeedback 。然后我有一些实现两个接口的UI对象 - 例如,实现它们的 Button 都有点击处理程序,并且也做了很好的反馈。

I have two interfaces, HasClickHandlers and DoesFancyFeedback. Then I have some UI objects that implement both interfaces - for example, a Button that implements both has click handlers and also does fancy feedback.

在我的代码中声明 Button s,我不想实际说 Button 因为也许以后我会希望它是,我不知道,图像有点击处理程序,并做了花哨的反馈。因此,不是具体而是说:

In my code that's declaring Buttons, I don't want to actually say Button because maybe later I'll want it to be, I dunno, an Image that has click handlers and does fancy feedback. So instead of being specific and saying something like:

Button saveButton = aButtonIPassedIn;
saveButton.addClickHandler();
saveButton.doFancyFeedback();

我想说,

{HasClickHandlers + DoesFancyFeedback} clickyFeedbackThing = aThingIPassedIn;
clickyFeedbackThing.addClickHandler();
clickyFeedbackThing.doFancyFeedback();

我希望编译器要求 aThingIPassedIn 实现 HasClickHandlers DoesFancyFeedback

I want the compiler to require that aThingIPassedIn implement both HasClickHandlers and DoesFancyFeedback.

我可以创建一个扩展这两个接口的接口,并使用它。有没有更容易/更简洁的方式?

I could create an interface that extends those two interfaces, and use that. Is there any easier/less verbose way?

推荐答案

我认为没有更好的方法可以做你想做的事情。
我只是建议你做以下事情。您可以创建接受需要2个接口的参数的方法(让我们称之为foo):

I do not think that there is a better way to do what you want.I just wanted to suggest you to do the following. You can create method (let's call it foo) that accepts argument that requires 2 interfaces:

<T extends HasClickHandlers & DoesFancyFeedback> void foo(T arg);

请注意2个接口之间的一个&符号。

Please pay attention on one ampersand between 2 your interfaces.

这篇关于在Java中指定多个接口的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:51