面向对象的程序设计中"接口"是个什么概念,怎么使用?在UML中怎么表示?

telescope 2002-12-19 06:17:16
如题..
...全文
501 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
marcal 2002-12-20
  • 打赏
  • 举报
回复
接口相对于类继承是十分容易理解的。

这里有一个十分容易而又可以区分二者的方法

类继承是一种对实现的继承
子类必定要包含了或者重载了父类的每一个方法。这通常代表着是“某类的一员的关系”

A Beagle IS A Dog.
小猎犬也是一种狗

接口继承有一点点不同。它更加想一种特性,一般用来指示对象有某种特性。如果你继承了接口,你就要自己做出这些特性的实现。一个接口是表现了对象能做什么,不表示对象是某种现实存在的东西的一个分类。

小猎犬也会“吠”的,所以他有会乱叫的特性

使用接口的其中一条理由是在版本升级的时候。
对祖先类的任何改变都将改变它的子类。
在使用接口继承的时候,你可以使用多重接口降低这个问题造成的影响。在使用接口的时候,自类只能看到父接口,但是没有实现,实现要自己做。
weimenren 2002-12-20
  • 打赏
  • 举报
回复
接口继承:
描述一种has a的关系。接口只是告诉你这个接口有这个能力,而没有具体提供实现。

类继承:
描述一种is a的关系。类继承的时间不光继承接口还继承实现。

telescope 2002-12-20
  • 打赏
  • 举报
回复
没看懂啊....
liuty2006 2002-12-19
  • 打赏
  • 举报
回复
对C++来说,接口就是抽象基类.
对java来说,接口就是Interface.

主要用来隔离client和具体实现,使得系统更容易扩展
telescope 2002-12-19
  • 打赏
  • 举报
回复
可否举例说明一下?
gmc007 2002-12-19
  • 打赏
  • 举报
回复
同意楼上,感觉用接口跟类比较就能更好的理解。
zfluo 2002-12-19
  • 打赏
  • 举报
回复
接口就是定义了一个类所能够接受的请求内容,规范了你能够对
该对象发出的请求。
在UML图中,接口就是方格中得public和protected的成员函数。
public接口可以接受所有外界的请求,
protected接口只能够接受该对象的继承者的请求
whxbb 2002-12-19
  • 打赏
  • 举报
回复
在国外的一个论坛上看到一个关于抽象类和接口比较的帖子,其中有一篇感觉很有道理:

Interface versus Class Inheritance is pretty easy once you understand it.

There is a very easy way to distinguish the 2.

Class inheritance = implemenation inheritance.
What this means is that if you have a base object you inherit from, your new derived object will either contain or override the existing functionality of the base object. This is often referred to as "is a" relationship.

A Beagle IS A Dog.


Interface inheritance is a little different. It is basically like a contract and is usually referred to as a "has a" relationship. By agreeing to implement an interface, you agree to implement all of its members, and provide your own implementation. An Interface should describe what a class does, rather than what a class is.

A Beagle Has A Bark

One reason to use Interfaces over Implementations is versioning or upgradability.
Any changes you make to your base class (abstract class) could possibly break subclasses that are derived from that class. By implementing interfaces you alleviate this problem by allowing clients or code to have multiple interfaces. By using interfaces, subclass can only see its parents interface, not the implementation.

LongWayToGo 2002-12-19
  • 打赏
  • 举报
回复
接口实质上就是对象访问协议,你必须按照接口的规定来访问对象。

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧