简体   繁体   English

为什么我们使用抽象类和接口? 类,Abstarct类和接口之间的区别

[英]Why we use abstract class and interface ? Difference between Class, Abstarct Class and Interface

I know this is very repetitive question i also read most of the post but not found the satisfactory answers. 我知道这是一个非常重复的问题,我也阅读了大部分帖子,但没有找到满意的答案。 I know the bookish difference which i read from most of the posts. 我知道我从大多数帖子中看到的书呆子差异。

Can any one please tell me 谁能告诉我

why we use abstract class and interface? 为什么我们使用抽象类和接口?

when we use this one? 当我们使用这个?

i know that when we have some repeated task but implementing in a different way each time then we use abstract class. 我知道,当我们有一些重复的任务但每次都以不同的方式实现时,我们将使用抽象类。

i think we can implement this by a normal class and subclass using overriding then why we use abstract? 我认为我们可以使用覆盖通过普通类和子类来实现,然后为什么要使用抽象? also their is a difference of access specifier between abstract class and interface as well as the compulsion of implementation in interface . 它们也是抽象类和接口之间的访问指定符的区别以及接口中实现强制性

Interface is quite acceptable that when we want to force to implement all method in that situation we use interface but why abstract class? 接口是完全可以接受的,当我们想要强制在这种情况下实现所有方法时,我们使用接口,但是为什么要使用抽象类呢? Is compulsion of implementation is just a difference? 强制实施仅仅是区别吗?

thanks in advance. 提前致谢。

Beautifully explained here - https://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface 精美的解释在这里-https://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

In a nutshell, 简而言之,

Interfaces are used to define the peripheral abilities of a class. 接口用于定义类的外围功能。 In other words both Human and Vehicle can inherit from a IMovable interface. 换句话说,人和车辆都可以从IMovable接口继承。

An abstract class defines the core identity of a class and there it is used for objects of the same type. 抽象类定义了一个类的核心身份,在这里它用于相同类型的对象。

Also take a look at https://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx 还可以看看https://msdn.microsoft.com/zh-cn/library/scsyfw1d(v=vs.71).aspx

Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components. 以下是一些建议,可以帮助您决定是使用接口还是抽象类为组件提供多态性。

  1. If you anticipate creating multiple versions of your component, create an abstract class. 如果您打算创建组件的多个版本,请创建一个抽象类。 Abstract classes provide a simple and easy way to version your components. 抽象类提供了一种简便的方式来对组件进行版本控制。 By updating the base class, all inheriting classes are automatically updated with the change. 通过更新基类,所有继承的类都会随着更改自动更新。 Interfaces, on the other hand, cannot be changed once created. 另一方面,接口一旦创建就无法更改。 If a new version of an interface is required, you must create a whole new interface. 如果需要新版本的接口,则必须创建一个全新的接口。

  2. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. 如果您要创建的功能将在各种不同的对象中有用,请使用界面。 Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. 抽象类应主要用于紧密相关的对象,而接口最适合为不相关的类提供通用功能。

  3. If you are designing small, concise bits of functionality, use interfaces. 如果您正在设计小的简洁功能,请使用接口。 If you are designing large functional units, use an abstract class. 如果要设计大型功能单元,请使用抽象类。

  4. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. 如果要在组件的所有实现之间提供通用的实现功能,请使用抽象类。 Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members. 抽象类允许您部分实现您的类,而接口不包含任何成员的实现。

Consider this. 考虑一下。

An interface has no implementation. 接口没有实现。 It forces prototypes onto a class but does not help implementing them. 它将原型强加到类上,但无助于实现它们。

An interface is not tied to a class hierarchy. 接口未绑定到类层次结构。 You can apply the same interface to multiple, entirely different class trees. 您可以将同一接口应用于多个完全不同的类树。 This is important and one of the things that an interface offers over an abstract class. 这很重要,也是接口通过抽象类提供的东西之一。 An interface is implementation independent, you can "slap it onto" any class. 接口是与实现无关的,您可以将其“扩展”到任何类上。 This is the most powerful aspect of an interface. 这是界面最强大的方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM