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

Keywords: C#

During this period of time, the project used interfaces. At first, it was not a special understanding of interfaces, but simply knew that the interface definition was very simple, and even thought that the interface was just a superfluous action (personal development time). Now start team development, only to find that the interface was so important and convenient!

Next, I'll talk about my shallow views on interface use during this period of time. I hope you all praise me. I hope you will include more suggestions when I say something wrong.

READY GO!

The definition of interface is not much to say, it has a very important knowledge point, that is, all inheritance of this interface class must implement the definition of interface, speaking of this must, in team development, as long as we agree on the interface, then our code is unified!!!?

This is the first point that I think interface is important: it is convenient for us to unify the project regulations, and for the management of team code!

Another example is given to illustrate:

A company decided to develop an animal system, which contains many animals. The company decided to achieve the screaming behavior of each animal.

At this point, we are generally programmers to get their own animals to achieve after the start of a bold work!!!

X programmer implements the dog class. He writes a shouting method void Han(){... }

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

M programmer realizes pig like this. He writes a screaming method void Shout(string content) {... }

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

All right, now we have finished the animals we need to finish. Next door, Lao Wang begins to realize the singing of all animals!!! &%*%%A rude How do I write this? One by one to call????

Let's see, X programmer's English is not very good, and there is not too much to control, just write the method of animal shouting, Y programmer and M programmer write the same name of the method of shouting, but M programmer also pass the content of animal shouting!!!!!

Next door Lao Wang now wants all animals to call once, and he has to call methods from one animal to another...

OK, next meeting to discuss, Lao Wang next door defines an animal interface, all animals have to inherit this interface, this interface only defines a void Shout(); (not too much writing, lazy)

After X,Y,M programmers inherited, X, M immediately found problems, and then began to change their own classes.

Then Lao Wang began to sing in unison! Ha ha ha ha ha ha ha

Next, I'll post the code for you to see.

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 shouting
        /// </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 Wang Wang");
        }
    }
}

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("cat");
        }
    }
}

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 did the pig come? Hog call");
        }
    }
}

Next door Lao Wang realizes all beasts sing in unison (overthrowing Lao Wang's existence)

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)
        {
            //All animals sing(Here you can use reflection to initialize all inheritance IAnimal All animals, I will not write this, mainly depends on 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 call it once.
            for (int i = 0; i < animals.Count; i++)
            {
                animals[i].Shout();
            }

            
        }
    }
}

I'm done with my rough idea of this interface. Interface is a very simple thing to use, but we still need to understand the function of this interface. I hope this article can make more novices like me take the first step towards interface.

The above motion map is provided by Tudouluo (Baidu)

Posted by wpsa on Thu, 23 May 2019 15:06:59 -0700