Java为什么接口扩展接口

编程入门 行业动态 更新时间:2024-10-21 19:44:50
本文介绍了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为什么接口扩展接口

本文发布于:2023-06-07 15:21:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!