首页 > 其他 > 详细

Deciding Between an Abstract Class and an Interface

时间:2014-03-13 12:21:28      阅读:351      评论:0      收藏:0      [点我收藏+]

An abstract class is a base class that may have some members not implemented in the base class, but only in the derived classes. An interface is just a contract that a class can choose to fulfill - a list fo member that it must implement if it implements the interface.

You‘ll often use an abstract class when you use nouns to describe the is-arelationship between a base class and derived classes. E.g. Person as an abstract class and Man and Woman as derived classes.

By contrast, you‘ll often use interfaces when you think about verbs that describe things that instances of a class can do. E.g. IRun interface containing Run method, implemented by Man and Woman, but also Horse classes.

bubuko.com,布布扣
bubuko.com,布布扣
1 public abstract class Person
2 // ...
3 
4 public class Man: Person, IOperateTools, IDriveTractor
5 // ...
6 
7 public class Woman: Person, IAskForDirections, ICollaborate
8 // ...
bubuko.com,布布扣
bubuko.com,布布扣

Deciding Between an Abstract Class and an Interface,布布扣,bubuko.com

Deciding Between an Abstract Class and an Interface

原文:http://www.cnblogs.com/cfdown/p/3597633.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!