简体   繁体   English

抽象类与接口

[英]Abstract class Vs Interface

An abstract class is the same thing as an interface except it is an actual class, not just a contract. 抽象类与接口相同,只是它是一个实际类,而不仅仅是合同。 Why interface is refered to as a contract ? 为什么接口被称为合同?

Both interface and an abstract class are contracts, since they bind you. 接口和抽象类都是契约,因为它们绑定了您。

But there other differences between an interface and an abstract class. 但是接口和抽象类之间还有其他区别。

Like you said, if you need an actual class, with content to the functions or actual data members with default values, or a ctor, then obviously you will need an abstract class. 就像您说的那样,如果您需要一个实际的类,该类具有函数的内容或具有默认值的实际数据成员或ctor,那么显然您将需要一个抽象类。

But many times, the choose between an interface or an abstract class will be technically the same. 但是很多时候,接口还是抽象类之间的选择在技术上都是相同的。 And sometimes even in the long term, it will be the same. 甚至从长远来看,有时也会一样。

Your decision then should be based on the nature of what you are looking. 然后,您的决定应基于所寻找内容的性质

Is it an extra character to your data types? 它是数据类型的额外字符吗? Or does it define what your data type is ? 还是定义您的数据类型什么?

I've tried to think of any real world example, but I couldn't find any as I haven't done anything with the subject for years now, so I'll give you a book-like example. 我尝试过考虑任何真实的示例,但是由于多年以来我都没有做任何事情,所以找不到任何示例,因此我将为您提供一个类似书的示例。

Say we have an abstract class Animal: 假设我们有一个抽象类Animal:

public abstract class Animal
{
    abstract string Name;
    abstract bool IsWild;
    abstract bool IsHappy;
}

This could have been easily an interface. 这本来可以是一个接口。

And choosing it to be an interface will actually won't have any negative effect on your design. 选择它作为接口实际上不会对您的设计产生任何负面影响。

But in the matter of the nature of it, it must be an abstract class, since class Dog is an animal. 但是就其性质而言,它必须是抽象类,因为class Dog 动物。 It must be class Dog : Animal rather than class Dog : IAnimal . 它必须是class Dog : Animal class Dog : IAnimal而不是class Dog : IAnimal

Being an animal is not an extra character of Dog . 做动物不是Dog的额外特征。 It what defines it. 定义了什么。

And you want the restrict all the animals to inherit only from Animal . 并且您希望限制所有动物仅从Animal继承。

IAnimal let someone inherit something else. IAnimal让某人继承其他东西。 like: 喜欢:

public class Dog : Food, IAnimal {}

Interestingly enough, sometimes one might write an abstract class with nothing at all , just for the sake of the same thing. 有趣的是,有时可能为了同一件事而写一无所有的抽象类。

For example, if there was no common property for any Animal you will still prefer to do: 例如,如果没有任何Animal共同财产,您仍然会喜欢:

public abstract class Animal { }

public class Dog : Animal // No actual added value.
{ 
        .........
}

An abstract class is the same thing as an interface Not true. 抽象类与接口是同一件事不正确。 An interface does not define method bodies, private methods, static fields/methods, etc. 接口未定义方法主体,私有方法,静态字段/方法等。

Abstract methods can be private ones and therefore are not guaranteed to be accessible to the outside world, interface implementations are obliged to expose the methods of the interface and thus are "guranteeing" the presence and accessibility of such methods, hence the term contract. 抽象方法可以是私有方法,因此不能保证可以被外界访问,接口实现必须公开接口方法,从而“保证”此类方法的存在和可访问性,因此称为合同。

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

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