使用抽象类和接口的优点 [英] Advantages Of Using Abstract class and Interface

查看:83
本文介绍了使用抽象类和接口的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用抽象类和接口有什么好处?



问候

Balamurugan

Hi,
What are the Advantages Of Using Abstract class and Interface ?

Regards
Balamurugan

推荐答案

阅读本文

抽象类与接口 [ ^ ]


我们不能在接口中定义字段,但可以在抽象类中定义字段。

接口有一个方法的签名,但抽象类可以包含两种类型的方法;它们具有签名或实现。

接口成员默认是公共的,不能使用访问说明符,但在抽象类中,我们可以为每个成员定义访问说明符。

接口很慢,因为它需要在相应的类中找到实际的方法。但是抽象类很快。

我们可以从单个类继承多个接口但不能继承多个抽象类。





以下是Herbert Schildt撰写的The Complete Reference C#2.0中的一句话:



当你能用术语完整描述这个概念时它做什么而不需要指定任何如何做,那么你应该使用一个接口。如果你需要包含一些实现细节,那么你需要在抽象类中表示你的概念。 br $> b $ b

http://social.msdn.microsoft.com/Forums/en-US/8ad621b8-a915-4d7e-89c3-5dbbc47202fd/whats-the -difference-between-abstract-classes-and-interfaces?forum = csharplanguage [ ^ ]
We can't define fields in an interface but can define fields in an abstract class.
The interface has a signature of methods but an abstract class can contain both types of methods; these have a signature or an implementation.
Interface members are by default public and can't use an access specifier for them but in an abstract class we can define an access specifier for each member.
An interface is slow because it needs to find the actual method in the corresponding classes. But an abstract class is fast.
We can inherit from multiple interfaces for a single class but can't inherit multiple abstract classes.


Here's a quote from "The Complete Reference C# 2.0" by Herbert Schildt that :

"When you can fully describe the concept in terms of "what it does" without needing to specify any of "how it does it", then you should use an interface. If you need to include some implementation details, then you will need to represent your concept in an abstract class."

http://social.msdn.microsoft.com/Forums/en-US/8ad621b8-a915-4d7e-89c3-5dbbc47202fd/whats-the-difference-between-abstract-classes-and-interfaces?forum=csharplanguage[^]


接口和抽象类允许您规划进化,即基于OOP的编程语言中的这些标准功能为您提供了在客户需要更多功能的后期阶段增强程序的选项。
Interfaces and Abstract classes allow you to plan for evolution i.e. these standard features in OOP based programming languages give you an option to enhance your program at a later stage when customer demands for more features.

这篇关于使用抽象类和接口的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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