接口和类之间的差异 [英] Difference Between Interface and Class
问题描述
由于标题状态我想知道使用类之间的区别如下
As the title states i want to know the difference between using the class like this
public class Account
{
public string Username { get; set; }
public string Password { get; set; }
}
并使用此接口
public class Account : IAccount
{
public string Username { get; set; }
public string Password { get; set; }
}
public interface IAccount
{
string Username { get; set; }
string Password { get; set; }
}
我真的很困惑,因为我发现接口是无用的,因为我可以做
I am really confused as i find interface is useless as all i can do with it can be done only using Class, Hence, i need someone to clarify things for me.
推荐答案
接口是一个 contract :它指定实现接口必须具有的类的成员(方法和属性)。但是因为它只是一个合同,它没有任何成员的实现。类可以实现零个,一个或多个接口。
An interface is a contract: it specifies what members (methods and properties) a class implementing the interface must have. But because it is only a contract, it has no implementations for any of its members. A class can implement zero, one or multiple interfaces.
相反,类是一个... well ... (如分类法)。例如, Animal
是一类生物,长颈鹿
是一类动物。继承表达这种关系:当
时, c是
Giraffe
继承自 Animal
。它可以做任何动物可以做的,和更多。它可以为其成员提供实现,在.NET中,一个类将继承自另外一个类(除非另有说明,否则 Object
)。
In contrast: a class is a... well... class of objects (like in taxonomy). For example, an Animal
is a class of living things, and a Giraffe
is a class of animals. Inheritance expresses this relationship: an Giraffe
is an Animal
when Giraffe
inherits from Animal
. It can do anything an animal can do, and more. It can provide implementations for its members, and in .NET a class will inherit from exactly one other class (which is Object
unless specified otherwise).
所以,如果你想表达你的类遵守一个或多个合同:使用 interfaces 。但是,您不能提供实现。如果你想表达你的类是某事,请扩展一个基类。在这种情况下,您可以提供一个实现,但只能扩展一个基类。
So, if you want to express that your class adheres to one or more contracts: use interfaces. However, you cannot provide an implementation. If you want to express that your class is something, extend a base class. In that case you can provide an implementation, but you can extend only one base class.
一个具体的例子:
链表,数组列表,堆栈和字典有一些共同点:它们代表一组元素。但是它们的实现是完全不同的。他们唯一共同的是他们坚持的合同: ICollection
。这意味着你的类可以请求一个集合,任何集合:任何实现 ICollection
,无论其实现。
A linked list, an array list, a stack and a dictionary have something in common: they represent a collection of elements. But their implementations are completely different. The only thing they have in common is the contract they adhere to: ICollection
. This means your classes can ask for a collection, any collection: anything that implements ICollection
, regardless of its implementation.
另一方面:汽车,摩托车和卡车也有一些共同点:它们是轮式车辆。但他们有更多的共同点:他们都有一个电机,他们都旋转他们的轮胎向前。基本上,它们是 Vehicle
类的对象的成员,并且可以共享(部分)它们的实现。但是,卡车
可能是车辆
和 CargoCarrier ,你不能在C#中表达。
On the other hand: a car, a motorcycle and a truck also have something in common: they are wheeled vehicles. But they have more in common than that: they all have a motor, they all spin their tires to go forward. Essentially, they are members of the Vehicle
class of objects, and can share (part of) their implementation. However, while a Truck
may be a Vehicle
and a CargoCarrier
, you cannot express this in C#.
这篇关于接口和类之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!