C # The interface further explores its role and is suitable for new users to understand,

Source: Internet
Author: User

C # The interface further explores its role and is suitable for new users to understand,

Preface

  In the previous article, I briefly described the functions of the c # interface and used a very bad example to explain it. One day later, after reading your comments, I checked the descriptions of c # interfaces by many predecessors in the forum. I found that it is not easy for new people to understand the examples. Next, I will explain it again from the perspective of my newbie. Hahahaha ......

Theoretical adaptation story

  Company A learned the lesson after the incident of "beeching", and old Wang began to use the interface to manage the project. To this end, he has made a lot of effort, so he has no other functions to explore the interface (no reflection on everyone, just to compile the story ).

On this day, Company A issued A new demand, which requires someone to take an animal out and bend around. When he saw this demand, he grayed out: I have an interface, but I am afraid of it. I want to change the interface so that small people can implement relevant behavior.

Old Wang added a void Walk (string people) in the interface, issuing commands, small ones, to quickly implement actions in various animals. I want to bend around! Hahaha

After the walking behavior of each animal was quickly completed, Old Wang began to implement the function of walking an animal.

Then he wrote the code like this:

  

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace InterfaceProject {// <summary> // person // </summary> public class People {// <summary> // name // </summary>> private string name; public string Name {get {return name;} set {name = value ;}# region public void tracking withanimal (Dog dog) {dog. walk (this. name);} public void Using withanimal (Cat cat) {cat. walk (this. name);} public void Using withanimal (Pig pig) {pig. walk (this. name) ;}# endregion }}View Code

In this way, it takes a while to secure the system. Suddenly one day, Company A came to A group of new animals and distributed them to various programmers. They quickly completed the various animals.

At this moment, the Old Wang dummies. There are more than 1000 animals in this Nima slot. I have to write 1000 things for walking !!!!! Days!

Old Wang now knows the problem and immediately finds a solution in Zenkey's blog. Finally, he finds this article (hahahahaha)

After seeking a solution, he immediately changed the code and went to the satisfaction of the solution ......

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace InterfaceProject {// <summary> // person // </summary> public class People {// <summary> // name // </summary>> private string name; public string Name {get {return name;} set {name = value;} public People (string name) {this. name = name ;}# region with animal routing public void without withanimal (IAnimal animal) {animal. walk (this. name);} // public void Using withanimal (Dog dog) // {// dog. walk (this. name); //} // public void Using withanimal (Cat cat) // {// cat. walk (this. name); //} // public void Using withanimal (Pig pig) // {// pig. walk (this. name); //} # endregion }}View Code

Let's take turns with all the animals !!!

 

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 the animals in the Old Wang band walk People p = new People ("Old Wang"); for (int I = 0; I <animals. count; I ++) {p. using withanimal (animals [I]) ;}}}

 

Summary

I don't know what you have learned based on this story. After this article, I will no longer explore the functions of interfaces. At present, I have made it easier to use these functions in my company, I hope all new users can find out the interface here.

If you cannot understand it, refer to the c # interface described in the previous article.

If you agree, please like it. If I have something wrong, I hope you can point out that we can make common progress.

  

 

 

  

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.