C# 接口和类的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15857982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference Between Interface and Class
提问by Daniel Eugen
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; }
}
and using and Interface like this
并像这样使用和界面
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.
我真的很困惑,因为我发现接口是无用的,因为我可以用它做的所有事情只能使用 Class 来完成,因此,我需要有人为我澄清事情。
采纳答案by Daniel A.A. Pelsmaeker
An interface is a contract: it specifies what members (methods and properties) a class implementing the interface musthave. 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.
接口是一种契约:它指定实现接口的类必须具有哪些成员(方法和属性)。但是因为它只是一个合约,它的任何成员都没有实现。一个类可以实现零个、一个或多个接口。
In contrast: a classis 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 anAnimal
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).
相比之下:一个类是……嗯……对象的类(就像在分类学中一样)。例如,anAnimal
是一类生物,aGiraffe
是一类动物。继承表达了这种关系: an Giraffe
is anAnimal
whenGiraffe
继承自Animal
。它可以做任何动物可以做的事情,甚至更多。它可以为其成员提供实现,并且在 .NET 中,一个类将完全继承自另一个类(Object
除非另有说明)。
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 issomething, extend a base class. In that case you can provide an implementation, but you can extend only onebase class.
所以,如果你想表达你的类遵守一个或多个契约:使用接口。但是,您不能提供实现。如果你想表达你的类是什么,扩展一个基类。在这种情况下,您可以提供一个实现,但您只能扩展一个基类。
For a concrete example:
举个具体的例子:
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.
链表、数组列表、堆栈和字典有一些共同点:它们代表元素的集合。但是它们的实现是完全不同的。他们唯一的共同点是他们遵守的合同:ICollection
。这意味着您的类可以要求一个集合,任何集合:任何实现 的东西ICollection
,而不管它的实现如何。
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
anda CargoCarrier
, you cannot express this in C#.
另一方面:汽车、摩托车和卡车也有一个共同点:它们都是轮式车辆。但他们还有更多的共同点:他们都有一个马达,他们都转动轮胎向前走。本质上,它们是Vehicle
对象类的成员,并且可以共享(部分)它们的实现。但是,虽然 aTruck
可能是 aVehicle
和a CargoCarrier
,但您不能在 C# 中表达它。
回答by Clint
Basically:
基本上:
An interface provides a
contract
specifying how to talk to an object, but not the specifics of how that object handles that request (apart from parameters and return types etc)
接口提供了
contract
如何与对象对话的指定,但没有提供该对象如何处理该请求的细节(除了参数和返回类型等)
And:
和:
A class (especially an abstract class) provides both the information on how to talk to an object, but in some cases the actual implementation (think overriding a method of a base class).
类(尤其是抽象类)既提供了如何与对象对话的信息,也提供了在某些情况下的实际实现(考虑覆盖基类的方法)。
Interfaces allow you to define a common form of communicating between objects, without caring about the specifics of how they do the things.
接口允许您定义对象之间的通用通信形式,而无需关心它们如何做事的细节。
An example would be logging:
一个例子是日志记录:
public interface ILog
{
void WriteMessage(string message);
}
public class ConsoleLogger : ILog
{
public void WriteMessage(string message)
{
Console.WriteLine(message);
}
}
public class MessageBoxLogger : ILog
{
public void WriteMessage(string message)
{
MessageBox.Show(message);
}
}
public void DoSomethingInteresting(ILog logger)
{
logger.WriteMessage("I'm doing something interesting!");
}
Because both ConsoleLogger
and MessageBoxLogger
implement the ILog
interface (the WriteMessage
method, any part of code can take an ILog
without ever needing to know what it actually does, they only care that it does something- in this case, writing a log message.
因为无论ConsoleLogger
和MessageBoxLogger
实现ILog
接口(该WriteMessage
方法,代码的任何部分都可以采取ILog
甚至无需知道它实际上做,他们只关心它的东西-在这种情况下,写日志信息。
So in the code above, either a ConsoleLogger
or MessageBoxLogger
could be supplied to DoSomethingInteresting
it doesn't matter at all because ILog
"knows" how to talk to that object.
所以在上面的代码中, a ConsoleLogger
orMessageBoxLogger
可以提供给DoSomethingInteresting
它根本无关紧要,因为ILog
“知道”如何与该对象交谈。