简体   繁体   English

Java为什么接口扩展接口

[英]Java why interface extends interface

I am wondering under what circumstances do we extend an interface from an interface?我想知道在什么情况下我们从一个接口扩展一个接口? Because, for example因为,例如

interface A{
    public void method1();
}
interface B extends A{
    public void method2();
}
class C implements B{
    @Override public void method1(){}
    @Override public void method2(){}
}

Isn't it equivalent to是不是相当于

interface A{
    public void method1();
}
interface B{
    public void method2();     
}
class C implements A, B{
    @Override public void method1(){}
    @Override public void method2(){}
}

Are there any significant reasons behind?背后有什么重要的原因吗?

The significant reasons depend entirely on what the interface is supposed to do.重要原因完全取决于界面应该做什么。

If you have an interface Vehicle and an interface Drivable it stands to reason that all vehicles are drivable.如果你有一个接口 Vehicle 和一个接口 Drivable ,那么所有的车辆都是可以驾驶的。 Without interface inheritance every different kind of car class is going to require如果没有接口继承,每种不同类型的汽车类都将需要

class ChevyVolt implements Vehicle, Drivable
class FordEscort implements Vehicle, Drivable
class ToyotaPrius implements Vehicle, Drivable

and so on.等等。

Like I said all vehicles are drivable so it's easier to just have:就像我说的所有车辆都可以驾驶,所以更容易拥有:

class ChevyVolt implements Vehicle
class FordEscort implements Vehicle
class ToyotaPrius implements Vehicle

With Vehicle as follows:配车如下:

interface Vehicle extends Drivable

And not have to think about it.而且不必考虑它。

Yes there is: it's like inheritance anywhere else.是的:它就像其他任何地方的继承。 If B is a specialization of A then it should be written that way.如果 BA 的特化,那么它应该这样写。 The second one indicates that the class just happens to implement 2 interfaces with no relationship between them.第二个表示该类恰好实现了 2 个接口,它们之间没有任何关系。

From the end result standpoint you could just use multiple interfaces in place of handling a hierarchy (just as you could avoid using inherited behavior in a class hierarchy).从最终结果的角度来看,您可以只使用多个接口来代替处理层次结构(就像您可以避免在类层次结构中使用继承行为一样)。 This would leave information more scattered, however, and as (if not more) significantly it obfuscates the intent of the software model.然而,这会使信息更加分散,并且(如果不是更多的话)显着地混淆了软件模型的意图。

Yes, there is one big difference.是的,有一个很大的不同。

In your first example, your new interface B is defined to extend from A, so going forward, any class that implements B automatically implements A. Basically, you're telling the compiler "here's what it means to be an A, here's what it means to be a B, and oh, by the way, all B's are also A's ."在您的第一个示例中,您的新接口 B 被定义为从 A 扩展,因此向前看,任何实现 B 的类都会自动实现 A。基本上,您是在告诉编译器“这就是成为 A 的意义,这就是它的意义意思是成为 B,哦,顺便说一句,所有 B 也是 A。 That lets you say things like...这让你可以说...

class C implements B {
    ... you implement all of the methods you need...
    ...blah...
    ...blah...
}
A myNewA = new C();

and that works fine.这很好用。

But, in your second example, you don't declare B to extend A, so the code above wouldn't work.但是,在你的第二个例子中,你没有声明 B 来扩展 A,所以上面的代码不起作用。 When you try to assign an instance of (the second kind of) C into a reference for A, it would complain, because you haven't told the complier that "all B's are really A's."当您尝试将(第二种)C 的实例分配给 A 的引用时,它会报错,因为您没有告诉编译器“所有 B 都是真正的 A”。 (ie there is no relation between B and C ) (即BC之间没有关系)

there is only one comment in里面只有一个评论

interface A{
    public void method1();
}
interface B extends A{
    public void method2();
}
class C implements B{
    @Override public void method1(){}
    @Override public void method2(){}
}

if you declare A a = new C();如果你声明A a = new C();

you CANNOT call method2();你不能调用method2(); because interface A knows nothing about elements in interface B even you implements methods in class C因为接口A对接口B中的元素一无所知,即使您实现了类C中的方法

If you don't want B to be implemented without implementing A you can make B extends A.如果你不想在不实现 A 的情况下实现 B,你可以让 B 扩展 A。

Example:例子:

interface LivingThing{
public void eat();
}

interface Dog extends LivingThing{
public void Bark();
}

It is possible to be a livingThing without being a dog but it is not possible to be a dog without being a livingThing.成为活物而不是狗是可能的,但不可能成为狗而不是活物。 So if it can bark it can also eat but the opposite is not always true.因此,如果它能吠叫,它也能吃东西,但事实并非总是如此。

Sometimes, you wouldn't want to allow being B without being A , but want to allow being A without necessarily being B .有时,您不想允许自己是B而不是A ,而是希望允许自己是A而不一定是B If A is Car and B is SportCar , you might want to use just Car interface to handle some flows, but sometimes need to be more specific and use explicitly SportCar .如果ACarBSportCar ,您可能只想使用Car接口来处理某些流,但有时需要更具体并明确使用SportCar This way, you cannot decide one day to implement just SportCar .这样,您就不能决定哪天只实施SportCar It forces you to implement Car as well.它也迫使您实施Car That's exactly the meaning of inheritance in OOP.这正是 OOP 中继承的含义。

Moreover, lets say you would want to declare a new class member implementing SportCar .此外,假设您想要声明一个实现SportCar的新类成员。 How would you make it also implement Car interface?你如何让它也实现Car接口? As far as I know, there is no way to add a type that consists of multiple interfaces.据我所知,没有办法添加由多个接口组成的类型。

public SportCar myCar;
// public SportCar,Car myCar -> invalid

This way you can't promise that myCar would have a method of Car (like Drive() ), which looses the advantage that was gained from inheritance.这样你就不能保证myCar会有Car的方法(比如Drive() ),它会失去从继承中获得的优势。

What you are saying is right, but it's not just about getting our work done, what if the requirement is like this:你说的是对的,但这不仅仅是为了完成我们的工作,如果需求是这样的呢:

1) Imagine that there are 10 interfaces, not designed at the same time. 1)假设有10个接口,不是同时设计的。 For eg the AutoCloseable interface in Java 7. A new auto close feature was added, it was not there till Java 6.例如 Java 7 中的 AutoCloseable 接口。添加了一个新的自动关闭功能,直到 Java 6 才出现。

2) If you designed an interface C which is a marker interface and you want all the classes derived from a given class B to be marked, the best solution would be to use B extends C and not going and putting the implements C everywhere. 2) 如果您设计了一个接口 C,它是一个标记接口,并且您希望从给定类 B 派生的所有类都被标记,那么最好的解决方案是使用 B extends C,而不是将实现 C 放在任何地方。

There are many more reasons.还有更多的原因。 If you look at the bigger picture of the classes and hierarchy, you might get the answer yourself.如果您查看类和层次结构的大图,您可能会自己得到答案。

Happy to Help Dharam乐于帮助达拉姆

The main difference here is that in your first example B is A and then some.这里的主要区别在于,在您的第一个示例中,B 是 A,然后是一些。 That means Interface B calls method1() and method2(), where as in the second one A and B are separate (interface B DOES NOT conclude method1()) and class C implements A and B. In both intances Class C will override method1() and method2().这意味着接口 B 调用方法 1() 和方法 2(),在第二个接口中,A 和 B 是分开的(接口 B 不包含方法 1()),类 C 实现 A 和 B。在这两种情况下,类 C 都将覆盖方法 1 () 和方法 2()。 I hope this helps!我希望这有帮助!

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

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