Talking about the c # interface is suitable for beginners to understand,

Source: Internet
Author: User

Talking about the c # interface is suitable for beginners to understand,

During this period of time, the project was useful to the interface. At first, it was not very easy to understand the interface, but only to know that the interface definition was very simple. I even thought that this interface was just an extra step (during personal development ). Now, when I started team development, I realized that the interface was so important and convenient!

Next, let's talk about my superficial opinions on the use of interfaces during this time period. I hope you will like them and I hope you will forgive me for your suggestions!

Ready go!

The interface definition is not much said. It has a very important knowledge point, that is, all that inherit this interface class must implement the interface definition. When it comes to this need, in the development of the team, as long as we have agreed on the interface, will our code be unified !!!

This is the first point that I think the interface is important: it makes it easy for us to unify the project rules and facilitate team code management!

Here is an example:

Company A decided to develop A set of animal systems, including many animals. The company decided to implement the shouting behavior of every animal ......

Speaking of this, we generally start to work with each programmer after they get the animal class they want to implement !!!

X programmers implement the Dog class. He writes a shouting method void Han (){......}

Programmer Y implements the cat class. He writes a shouting method void Shout (){......}

M programmer implements the pig class. He writes a shouting method void Shout (string content ){......}

..................

Now, all the animals that need to be done have been completed. The old king next door began to make the beam !!!! & ¥ % ¥ * % ¥ A rough opening! How can this be written? Call them one by one ???

Let's take a look at it. Programmers X are not very good at English and don't care too much about it. They just write out animal shouting methods. The names of shouting Methods written by programmers Y and M are the same, but M programmers still need to pass the animal shouting content !!!!!

Old Wang next door now wants to make all animals call it once, so an animal can call the method ......

OK. Let's discuss in the next meeting. Old Wang next door defines an animal interface. All animal classes have to inherit this interface. This interface only defines one void Shout, secretly lazy)

After the inheritance of the X, Y, and M programmers, X and M immediately found a problem and began to change the class in their hands.

At this moment, Old Wang began to hear about beasts! Haha

Next, paste the code.

Interface

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace InterfaceProject {// <summary> // animal interface // </summary> interface IAnimal {// <summary> // animal scream // </ summary> void Shout ();}}

Dog

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace InterfaceProject {// <summary> // Dog // </summary> public class Dog: IAnimal {public void Shout () {Console. writeLine ("Wang Wangwang ");}}}

Cat

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace InterfaceProject {// <summary> // Cat // </summary> public class Cat: IAnimal {public void Shout () {Console. writeLine (" ");}}}

Pig

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace InterfaceProject {// <summary> /// Pig // </summary> public class Pig: IAnimal {public void Shout () {Console. writeLine ("How do pigs call it ?? Pig name ");}}}

Next door, Old Wang came to realize the sound of beasts (to defeat the existence of Old Wang)

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace InterfaceProject {class Program {static void Main (string [] args) {// beam (reflection can be used to initialize all animals that inherit IAnimal, I don't want to write this. I mainly look at the Interface) List <IAnimal> animals = new List <IAnimal> (); IAnimal dog = new Dog (); animals. add (dog); IAnimal cat = new Cat (); animals. add (cat); IAnimal pig = new Pig (); animals. add (pig); // all animals are called once for (int I = 0; I <animals. count; I ++) {animals [I]. shout ();}}}}

I have just finished my rough understanding of this interface! Although this interface is easy to use, we still need to understand the role of this interface. I hope this article will allow more new users like me to take the first step towards this interface.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.