Difference Between Interface and Class [duplicate]

2019-02-05 04:49发布

This question already has an answer here:

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.

2条回答
乱世女痞
2楼-- · 2019-02-05 05:19

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#.

查看更多
男人必须洒脱
3楼-- · 2019-02-05 05:19

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.

查看更多
登录 后发表回答