Home >Backend Development >C#.Net Tutorial >Detailed explanation of solutions to c# interface problems
During this period, the project used interfaces. I didn’t particularly understand interfaces at first. I just knew that the interface definition was very simple. I even felt that this interface was just superfluous (when I was developing personally). Now that I started team development, I discovered how important and convenient the interface is!
Next, let me talk about my rough opinions on the use of interfaces during this period. I hope everyone will like what I said is right. I hope everyone will forgive me and give me suggestions if I am wrong!
READY GO!
I won’t go into details about the definition of the interface. It has a very important knowledge point, that is, all those that inherit this interface class must implement the definition in the interface. Speaking of This is necessary. In team development, as long as we agree on the interface, will our code be unified? ! !
This is the first point I think is important about interfaces: it allows us to unify project regulations and facilitate team code management!
Let’s use another example to illustrate:
Company A decided to develop an animal system, which contains many animals. The company decided to realize the shouting behavior of each animal...
Speaking of this, we usually have each programmer start working hard after getting the animal class they want to implement! ! !
Programmer {……}
M programmer implements the pig class and writes a shout method void Shout(string content){……}
………………
Okay, now that everyone has completed the animals they need to complete, Lao Wang next door starts to realize the roar of the beasts! ! ! ! &¥%¥*%¥¥%¥ A meal of foul language broke out! How to write this? Call them one by one? ? ?
Let’s take a look. Programmer The M programmer also needs to deliver the content of animal cries! ! ! ! !
Now that Mr. Wang next door wants to call all the animals, he has to call the method one by one...
OK, let’s hold a meeting to discuss, and Mr. Wang next door defines an animal interface. All animal classes must inherit this interface. This interface only defines one void Shout(); (I don’t want to write too much, it’s lazy)
After X, Y, M programmers inherit, X, M immediately discovered that there was a problem, and then began to change the class in his hand
At this time, Lao Wang started to roar with all the beasts! Hahahahaha
The code will be posted next for everyone to see
Interface
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject { /// <summary> /// 动物接口 /// </summary> interface IAnimal { /// <summary> /// 动物叫喊 /// </summary> void Shout(); } }
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject { /// <summary> /// 狗 /// </summary> public class Dog:IAnimal { public void Shout() { Console.WriteLine("汪汪汪"); } } }
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject { /// <summary> /// 猫 /// </summary> public class Cat:IAnimal { public void Shout() { Console.WriteLine("喵喵喵"); } } }
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject { /// <summary> /// 猪 /// </summary> public class Pig:IAnimal { public void Shout() { Console.WriteLine("猪怎么叫来着??猪叫"); } } }
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) { //百兽齐鸣(这里可以使用反射来初始化所有继承IAnimal的所有动物,我就不写这个了,主要看接口) 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); //所有动物都叫一遍 for (int i = 0; i < animals.Count; i++) { animals[i].Shout(); } } } }
The above is the detailed content of Detailed explanation of solutions to c# interface problems. For more information, please follow other related articles on the PHP Chinese website!