开发者

Understanding interface definition from C# book [closed]

开发者 https://www.devze.com 2023-01-24 03:45 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

Improve this question

I'm currently studying the Inheritance chapter of a C# book. I'm unab开发者_开发知识库le to understand the following line.

Interfaces enable you to separate the definition of objects from their implementation so that the objects can evolve without the risk of introducing incompatibility in existing applications.

Could anyone help me to understand what that line of text is saying with an example?


Interfaces enable you to separate the definition of objects from 
their implementation 

Definition of objects in this case refers to the publicly exposed part of an object. In the example below, it refers to the public properties Name / Age and the Public methods Insert and Update. In the below class, the definition of Person and its implementation (code used to implement the properties and methods) is the same i.e the class Person.

Class Person 
{
  private string _name; //private fields
  private string _age;  //private fields

  public Name {get {....} set {.....}}  //public properties
  public Age {get {....} set {.....}}  //public properties

  public void Insert() {.......} //public methods
  public void Update() {.......} //public methods
}

NOTE: "........" represents actual code which will be called when these properties / methods are invoked i.e their implementation.

Now, if i were to do create an interface for this called

Interface IPerson 
{
  public Name get; set;  //public properties
  public Age get; set;   //public properties
  public void Insert(); //public methods
  public void Update(); //public methods
}

and then do Class Person : IPerson then what I have done is that i have now separated the DEFINITION i.e. IPerson from the Implementation i.e Person.

NOTE : Why this is useful is already covered by Aamir and Gravell in their answers above so i wont repeat the same


Well, I'm not sure I'd entirely agree with the line - after all, you can still put a bug in the implementation - but the point is abstraction; if all you know about is that IAnimal has a Speak() method, you don't need to know about Dog, Cat, etc; and you don't need to know when somebody swaps Dog for DogFacade, LazyLoadedDog, etc. As long as the thing they give you is an IAnimal, you'll be happy - and won't need to change the downstream code.

(and if the Dog class wants to expose a Bark() method instead, then that is fine; it can have Speak as an explicit implementation of IAnimal.Speak)


This means that Interfaces allow you to define the basic functions of a set of things. How they are going to perform those functions is left to the things themselves. For example, a human can walk but how a male, female, old person, child etc walks is different but we can say that they all walk.

So, an interface is a group of functions put together which can be implemented differently by different classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消