本文介绍了Java为什么接口扩展接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道在什么情况下我们从接口扩展接口?因为,例如
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(){} }不等于
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 a interface Vehicle and an interface Drivable it stands to reason that all vehicles are 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依此类推。
就像我说的所有车辆都是可驾驶的,因此更容易拥有:
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使用车辆如下:
interface Vehicle extends Drivable而不必考虑它。
更多推荐
Java为什么接口扩展接口
发布评论