Java 实现多个接口的类的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6445475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:59:44  来源:igfitidea点击:

Type of class which implements multiple interfaces

javainterface

提问by Timo Willemsen

I'm currently trying to write some classes and I come accross the following problem:

我目前正在尝试编写一些类,但遇到了以下问题:

I'm trying to write a class that implements some functionality from two interfaces

我正在尝试编写一个从两个接口实现某些功能的类

interface IA {
 public void doSomething();
}

interface IB {
 public void doSomethingToo();
}

And I have two classes implementing those interfaces:

我有两个类实现这些接口:

class FooA implements IA, IB{

}

class FooB implements IA, IB{

}

Is there I way I can do this:

我有没有办法做到这一点:

public <type> thisClassImGonnaUse = returnEitherFooAOrFooB();
thisClassImGonnaUse.doSomething();
thisClassImGonnaUse.doSomethingToo();

采纳答案by jontro

Do a new interface

做一个新界面

public interface IAandB extends IA, IB 

and make FooA and FooB implement it instead.

并让 FooA 和 FooB 实现它。

回答by Bala R

you can create an abstract base class or another interface that extends both IA and IB

您可以创建一个抽象基类或另一个扩展 IA 和 IB 的接口

abstract class Base implements IA, IB{}

and

class FooA extends Base implements IA, IB{...}

回答by Michael McGowan

This might not be exactly what you want, but you could make a 3rd interface that extends both of them:

这可能不是你想要的,但你可以制作一个扩展它们的第三个接口:

interface IAandB extends A, B {}

Then FooAand FooBwould implement IAandBinstead of IAand IBdirectly:

然后,FooAFooB将执行IAandB的,而不是IAIB直接:

class FooA implements IAandB{}
class FooB implements IAandB{}

Then you can declare thisClassImGonnaUseto be of type IAandB:

然后你可以声明thisClassImGonnaUse为类型IAandB

public IAandB thisClassImGonnaUse = returnEitherFooAorFooB();
thisClassImGonnaUse.doSomething();
thisClassImGonnaUse.doSomethingToo();

回答by Waldheinz

If there is the possibility that a class implementes IAandIB, and this fact makes conceptually sense for the problem domain you're working in, the just have a IABlike this

如果一个类有可能实现IAandIB,并且这个事实在概念上对您正在处理的问题域有意义,那么只需IAB像这样

interface IAB extends IA, IB { /* nothing */ }

Do determine ifit makes sense in your case might be as simple as asking yourself what would be a good name for IAB?If you can come up with a satisfactory answer to this, you can just add that interface. If not, then the right place for your two

请确定是否是有意义的,你的情况可能是那样简单问自己,这将是对IAB一个好名字?如果您能对此提出满意的答案,则只需添加该接口即可。如果没有,那么适合你们两个的地方

thisClassImGonnaUse.doSomething();
thisClassImGonnaUse.doSomethingToo();

lines would be a method of FooAor FooB(or an base class of these two). This way you don't have to make casts or guesses about the nature of the classes that implement your interfaces (which might become invalid or troublesome to handle later).

行将是FooAor的方法FooB(或这两个的基类)。这样您就不必对实现您的接口的类的性质进行强制转换或猜测(这可能会变得无效或以后处理起来很麻烦)。