什么时候需要接口/抽象类? [英] When I need Interface/Abstract Class?

查看:79
本文介绍了什么时候需要接口/抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您对界面/抽象类的专家建议吗?一个小的实时示例,您认为interface/Abstract类很有意义.请不要建议我任何链接,我想要您的意见/想法...

请帮助:)

I want to know your expert advice on interface/abstract class? A small real time example, Where you think interface/Abstract class makes sense. Please don''t suggest me any link i want your views/ideas...

Please help :)

推荐答案

请参见
我的答案


请点击以下链接:

http://程序员.stackexchange.com/questions/41740/when-to-use-abstract-classes-in-of-interfaces-and-extension-methods-in-c [ http://codeofdoom .com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/ [
Hi, follow below links:

http://programmers.stackexchange.com/questions/41740/when-to-use-abstract-classes-instead-of-interfaces-and-extension-methods-in-c[^]


http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/[^]


使用抽象类:
当创建一个将广泛分发或重用的类库时(尤其是对客户端而言),应优先使用抽象类而不是接口.因为,它简化了版本控制.这是Microsoft团队开发基类库的做法. (COM是围绕接口设计的.)

1.使用抽象类为一系列类型定义一个公共基类.
2.使用抽象类提供默认行为.
3.在该逻辑上属于该层次结构的层次结构中,仅将其作为基类的子类.

使用界面:
创建一个可以随意更改的独立项目时,请优先使用接口,而不要使用抽象类.因为,它提供了更多的设计灵活性.

1.使用接口无需子类即可引入多态行为并为多重继承建模—允许特定类型支持多种行为.
2.使用接口为值类型设计多态层次结构.
3.当确实打算使用不可变合同时,请使用接口.
精心设计的界面定义了非常特定的功能范围.拆分包含无关功能的接口.

有关更多信息,请参见:
http://mycodelines .wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/ [
Use an abstract class :
When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)

1. Use an abstract class to define a common base class for a family of types.
2. Use an abstract class to provide default behavior.
3. Subclass only a base class in a hierarchy to which the class logically belongs.

Use an interface :
When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.

1. Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
2. Use an interface to design a polymorphic hierarchy for value types.
3. Use an interface when an immutable contract is really intended.
A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

for more refer:http://mycodelines.wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/[^]


这篇关于什么时候需要接口/抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆