﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-SoulEngineer</title><link>http://www.blogjava.net/beflyabot/</link><description>SoulEngineer</description><language>zh-cn</language><lastBuildDate>Thu, 07 May 2026 00:47:06 GMT</lastBuildDate><pubDate>Thu, 07 May 2026 00:47:06 GMT</pubDate><ttl>60</ttl><item><title>接口和抽象类的区别 (转载)</title><link>http://www.blogjava.net/beflyabot/archive/2011/10/14/361317.html</link><dc:creator>JavaDream</dc:creator><author>JavaDream</author><pubDate>Fri, 14 Oct 2011 13:42:00 GMT</pubDate><guid>http://www.blogjava.net/beflyabot/archive/2011/10/14/361317.html</guid><wfw:comment>http://www.blogjava.net/beflyabot/comments/361317.html</wfw:comment><comments>http://www.blogjava.net/beflyabot/archive/2011/10/14/361317.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/beflyabot/comments/commentRss/361317.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/beflyabot/services/trackbacks/361317.html</trackback:ping><description><![CDATA[<p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">我想，对于各位使用面向对象编程语言的程序员来说，&#8220;接口&#8221;这个名词一定不陌生，但是不知各位有没有这样的疑惑：接口有什么用途？它和抽象类有什么区别？能不能用抽象类代替接口呢？而且，作为程序员，一定经常听到&#8220;面向接口编程&#8221;这个短语，那么它是什么意思？有什么思想内涵？和面向对象编程是什么关系？本文将一一解答这些疑问。</p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><strong style="margin: 0px; padding: 0px;">1.面向接口编程和面向对象编程是什么关系</strong></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">首先，面向接口编程和面向对象编程并不是平级的，它并不是比面向对象编程更先进的一种独立的编程思想，而是附属于面向对象思想体系，属于其一部分。或者说，它是面向对象编程体系中的思想精髓之一。</p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><strong style="margin: 0px; padding: 0px;">2.接口的本质</strong></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">接口，在表面上是由几个没有主体代码的方法定义组成的集合体，有唯一的名称，可以被类或其他接口所实现（或者也可以说继承）。它在形式上可能是如下的样子：</p><div style="margin: 0px; padding: 4px 5px 4px 4px; border: 1px solid #cccccc; width: 961px; font-size: 13px; background-color: #eeeeee;"><img style="border-width: 0px; margin: 0px; padding: 0px;" alt="图片" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" orgsrc="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 19px;">interface</span><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;"> InterfaceName<br style="margin: 0px; padding: 0px;" /><img style="border-width: 0px; margin: 0px; padding: 0px;" alt="图片" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" orgsrc="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" /></span><span style="margin: 0px; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;">{<br style="margin: 0px; padding: 0px;" /><img style="border-width: 0px; margin: 0px; padding: 0px;" alt="图片" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" orgsrc="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" />    </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 19px;">void</span><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;"> Method1();<br style="margin: 0px; padding: 0px;" /><img style="border-width: 0px; margin: 0px; padding: 0px;" alt="图片" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" orgsrc="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" />    </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 19px;">void</span><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;"> Method2(</span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 19px;">int</span><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;"> para1);<br style="margin: 0px; padding: 0px;" /><img style="border-width: 0px; margin: 0px; padding: 0px;" alt="图片" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" orgsrc="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" />    </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 19px;">void</span><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;"> Method3(</span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 19px;">string</span><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;"> para2,</span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 19px;">string</span><span style="margin: 0px; padding: 0px; color: #000000; line-height: 19px;"> para3);<br style="margin: 0px; padding: 0px;" /><img style="border-width: 0px; margin: 0px; padding: 0px;" alt="图片" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" orgsrc="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" bloaded="true" />}</span></span></div><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> </p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">那么，接口的本质是什么呢？或者说接口存在的意义是什么。我认为可以从以下两个视角考虑：</p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; color: red; line-height: 19px;"><strong style="margin: 0px; padding: 0px;">1）接口是一组规则的集合，它规定了实现本接口的类或接口必须拥有的一组规则。体现了自然界&#8220;如果你是&#8230;&#8230;则必须能&#8230;&#8230;&#8221;的理念。</strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> 例如，在自然界中，人都能吃饭，即&#8220;如果你是人，则必须能吃饭&#8221;。那么模拟到计算机程序中，就应该有一个IPerson（习惯上，接口名由&#8220;I&#8221;开头）接口，并有一个方法叫Eat()，然后我们规定，每一个表示&#8220;人&#8221;的类，必须实现IPerson接口，这就模拟了自然界&#8220;如果你是人，则必须能吃饭&#8221;这条规则。</p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">从这里，我想各位也能看到些许面向对象思想的东西。面向对象思想的核心之一，就是模拟真实世界，把真实世界中的事物抽象成类，整个程序靠各个类的实例互相通信、互相协作完成系统功能，这非常符合真实世界的运行状况，也是面向对象思想的精髓。</p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; color: red; line-height: 19px;"><strong style="margin: 0px; padding: 0px;">2）接口是在一定粒度视图上同类事物的抽象表示。注意这里我强调了在一定粒度视图上，因为&#8220;同类事物&#8221;这个概念是相对的，它因为粒度视图不同而不同。</strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> 例如，在我的眼里，我是一个人，和一头猪有本质区别，我可以接受我和我同学是同类这个说法，但绝不能接受我和一头猪是同类。但是，如果在一个动物学家眼里，我和猪应该是同类，因为我们都是动物，他可以认为&#8220;人&#8221;和&#8220;猪&#8221;都实现了IAnimal这个接口，而他在研究动物行为时，不会把我和猪分开对待，而会从&#8220;动物&#8221;这个较大的粒度上研究，但他会认为我和一棵树有本质区别。</p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">现在换了一个遗传学家，情况又不同了，因为生物都能遗传，所以在他眼里，我不仅和猪没区别，和一只蚊子、一个细菌、一颗树、一个蘑菇乃至一个SARS病毒都没什么区别，因为他会认为我们都实现了ID<span style="margin: 0px; padding: 0px; line-height: 19px;">escendable这个接口（注：descend vi. 遗传），即我们都是可遗传的东西，他不会分别研究我们，而会将所有生物作为同类进行研究，在他眼里没有人和病毒之分，只有可遗传的物质和不可遗传的物质。但至少，我和一块石头还是有区别的。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">可不幸的事情发生了，某日，地球上出现了一位伟大的人，他叫列宁，他在熟读马克思、恩格斯的辩证唯物主义思想巨著后，颇有心得，于是他下了一个著名的定义：所谓物质，就是能被意识所反映的客观实在。至此，我和一块石头、一丝空气、一条成语和传输手机信号的电磁场已经没什么区别了，因为在列宁的眼里，我们都是可以被意识所反映的客观实在。如果列宁是一名程序员，他会这么说：所谓物质，就是所有同时实现了&#8220;IReflectabe&#8221;和&#8220;IE<span style="margin: 0px; padding: 0px; line-height: 19px;">sse</span>&#8221;两个接口的类所生成的实例。（注：reflect v. 反映  esse n. 客观实在）</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">也许你会觉得我上面的例子像在瞎掰，但是，这正是接口得以存在的意义。面向对象思想和核心之一叫做多态性，什么叫多态性？说白了就是在某个粒度视图层面上对同类事物不加区别的对待而统一处理。而之所以敢这样做，就是因为有接口的存在。像那个遗传学家，他明白所有生物都实现了ID<span style="margin: 0px; padding: 0px; line-height: 19px;">escendable</span>接口，那只要是生物，一定有Descend（）这个方法，于是他就可以统一研究，而不至于分别研究每一种生物而最终累死。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">可能这里还不能给你一个关于接口本质和作用的直观印象。那么在后文的例子和对几个设计模式的解析中，你将会更直观体验到接口的内涵。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;"><strong style="margin: 0px; padding: 0px;">3.面向接口编程综述</strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">通过上文，我想大家对接口和接口的思想内涵有了一个了解，那么什么是面向接口编程呢？我个人的定义是：<strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: red; line-height: 19px;">在系统分析和架构中，分清层次和依赖关系，每个层次不是直接向其上层提供服务（即不是直接实例化在上层中），而是通过定义一组接口，仅向上层暴露其接口功能，上层对于下层仅仅是接口依赖，而不依赖具体类</span>。</strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">这样做的好处是显而易见的，首先对系统灵活性大有好处。当下层需要改变时，只要接口及接口功能不变，则上层不用做任何修改。甚至可以在不改动上层代码时将下层整个替换掉，就像我们将一个WD的60G硬盘换成一个希捷的160G的硬盘，计算机其他地方不用做任何改动，而是把原硬盘拔下来、新硬盘插上就行了，因为计算机其他部分不依赖具体硬盘，而只依赖一个IDE接口，只要硬盘实现了这个接口，就可以替换上去。从这里看，程序中的接口和现实中的接口极为相似，所以我一直认为，接口（interface）这个词用的真是神似！</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">使用接口的另一个好处就是不同部件或层次的开发人员可以并行开工，就像造硬盘的不用等造CPU的，也不用等造显示器的，只要接口一致，设计合理，完全可以并行进行开发，从而提高效率。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">本篇文章先到这里。最后我想再啰嗦一句：面向对象的精髓是模拟现实，这也可以说是我这篇文章的灵魂。所以，多从现实中思考面向对象的东西，对提高系统分析设计能力大有脾益。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">下篇文章，我将用一个实例来展示接口编程的基本方法。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">而第三篇，我将解析经典设计模式中的一些面向接口编程思想，并解析一下.NET分层架构中的面向接口思想。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;"><strong style="margin: 0px; padding: 0px; color: #0000ff;">对本文的补充：</strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; color: #808080; line-height: 19px;">仔细看了各位的回复，非常高兴能和大家一起讨论技术问题。感谢给出肯定的朋友，也要感谢提出意见和质疑的朋友，这促使我更深入思考一些东西，希望能借此进步。在这里我想补充一些东西，以讨论一些回复中比较集中的问题。</span></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;"><strong style="margin: 0px; padding: 0px;">1.关于&#8220;面向接口编程&#8221;中的&#8220;接口&#8221;与具体面向对象语言中&#8220;接口&#8221;两个词</strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">看到有朋友提出&#8220;面向接口编程&#8221;中的&#8220;接口&#8221;二字应该比单纯编程语言中的interface范围更大。我经过思考，觉得很有道理。这里我写的确实不太合理。我想，面向对象语言中的&#8220;接口&#8221;是指具体的一种代码结构，例如C#中用interface关键字定义的接口。而&#8220;面向接口编程&#8221;中的&#8220;接口&#8221;可以说是一种从软件架构的角度、从一个更抽象的层面上指那种用于隐藏具体底层类和实现多态性的结构部件。从这个意义上说，如果定义一个抽象类，并且目的是为了实现多态，那么我认为把这个抽象类也称为&#8220;接口&#8221;是合理的。但是用抽象类实现多态合理不合理？在下面第二条讨论。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">概括来说，我觉得两个&#8220;接口&#8221;的概念既相互区别又相互联系。<strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #ff0000; line-height: 19px;">&#8220;</span><span style="margin: 0px; padding: 0px; color: #ff0000; line-height: 19px;">面向接口编程&#8221;中的接口是一种思想层面的用于实现多态性、提高软件灵活性和可维护性的架构部件，而具体语言中的&#8220;接口&#8221;是将这种思想中的部件具体实施到代码里的手<span style="margin: 0px; padding: 0px; color: #ff0000; line-height: 19px;">段。</span></span></strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; color: #ff0000; line-height: 19px;"></span><strong style="margin: 0px; padding: 0px;">2.关于抽象类与接口</strong></span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">看到回复中这是讨论的比较激烈的一个问题。很抱歉我考虑不周没有在文章中讨论这个问题。我个人对这个问题的理解如下：</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">如果单从具体代码来看，对这两个概念很容易模糊，甚至觉得接口就是多余的，因为单从具体功能来看，除多重继承外（C#，Java中），抽象类似乎完全能取代接口。但是，难道接口的存在是为了实现多重继承？当然不是。<span style="margin: 0px; padding: 0px; color: #ff0000; line-height: 19px;"><strong style="margin: 0px; padding: 0px;">我认为，抽象类和接口的区别在于使用动机。使用抽象类是为了代码的复用，而使用接口的动机是为了实现多态性。</strong></span>所以，如果你在为某个地方该使用接口还是抽象类而犹豫不决时，那么可以想想你的动机是什么。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">看到有朋友对IPerson这个接口的质疑，我个人的理解是，IPerson这个接口该不该定义，关键看具体应用中是怎么个情况。如果我们的项目中有Women和Man，都继承Person，而且Women和Man绝大多数方法都相同，只有一个方法DoSomethingInWC（）不同（例子比较粗俗，各位见谅），那么当然定义一个AbstractPerson抽象类比较合理，因为它可以把其他所有方法都包含进去，子类只定义DoSomethingInWC（），大大减少了重复代码量。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">但是，如果我们程序中的Women和Man两个类基本没有共同代码，而且有一个PersonHandle类需要实例化他们，并且不希望知道他们是男是女，而只需把他们当作人看待，并实现多态，那么定义成接口就有必要了。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">总而言之，接口与抽象类的区别主要在于使用的动机，而不在于其本身。而一个东西该定义成抽象类还是接口，要根据具体环境的上下文决定。</span></p><p style="padding: 0px; line-height: 19px; text-indent: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><span style="margin: 0px; padding: 0px; line-height: 19px;">再者，我认为接口和抽象类的另一个区别在于，抽象类和它的子类之间应该是一般和特殊的关系，而接口仅仅是它的子类应该实现的一组规则。（当然，有时也可能存在一般与特殊的关系，但我们使用接口的目的不在这里）如，交通工具定义成抽象类，汽车、飞机、轮船定义成子类，是可以接受的，因为汽车、飞机、轮船都是一种特殊的交通工具。再譬如Icomparable接口，它只是说，实现这个接口的类必须要可以进行比较，这是一条规则。如果Car这个类实现了Icomparable，只是说，我们的Car中有一个方法可以对两个Car的实例进行比较，可能是比哪辆车更贵，也可能比哪辆车更大，这都无所谓，但我们不能说&#8220;汽车是一种特殊的可以比较&#8221;，这在文法上都不通。</span></p><img src ="http://www.blogjava.net/beflyabot/aggbug/361317.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/beflyabot/" target="_blank">JavaDream</a> 2011-10-14 21:42 <a href="http://www.blogjava.net/beflyabot/archive/2011/10/14/361317.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>