Difference Between Interface and Class
This question already has an answer here:
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.
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).
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.
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.
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#.
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)
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.
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.
上一篇: 抽象和界面在PHP中有什么区别?
下一篇: 接口和类的区别