Talk about C interface problems. Beginners come and watch

  • 2021-11-01 04:26:58
  • OfStack

During this period of time, the project used interfaces. At first, I didn't particularly understand interfaces. I just knew that the definition of interfaces was very simple, and even felt that this interface was only one more example (when I developed it personally). Now start team development, only to find that the interface is so important and convenient!

Next, let's talk about my superficial views on the use of interfaces during this period of time. I hope everyone will praise what I said, and I hope everyone will forgive me for what I said wrong.

READY GO!

The definition of the interface is not much to say, it has a very important knowledge point, that is, all those who inherit this interface class must realize the definition in the interface. When it comes to this must, in the team development, as long as we agree on the interface, our code will be unified! ! !

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

Let's use another example to illustrate:

A decided to develop an animal system, which contains many animals. The company decided to realize the shouting behavior of each animal...
Having said that, we 1 is that each programmer gets the animals he wants to realize, and then he starts to do it drastically! ! !
The X programmer implements the dog class by writing a shouting method void Han () {…}
The Y programmer implements the cat class, and he writes a shouting method void Shout () {…}
The M programmer implements the pig class, and he writes a shouting method void Shout (string content) {…}
............

Ok, now that all the animals they need to complete, Lao Wang next door began to realize that all the animals are singing together! ! ! ! & $% $*% $% $1 swearing burst out! How do you write this? Call one by one? ? ?

Let's take a look. X programmers' English is not very good, and they don't care too much. They just write the method of animal shouting. The name of shouting method written by Y programmers and M programmers is 1, but M programmers have to pass on the content of animal shouting! ! ! ! !

Lao Wang next door now wants all animals to call once, and one animal and one animal have to call the method...

OK, next meeting to discuss, Lao Wang next door defines an animal interface, all animal classes have to inherit this interface, this interface only defines an void Shout (); (Just don't write too much, secretly lazy)

After X, Y and M programmers inherited, X and M immediately found that there was a problem, and then began to change their own classes

At this time, Lao Wang began to sing with all the animals! Ha ha ha ha

Next, post the code for everyone 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>
    ///  Animals shout 
    /// </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(" Woof, woof, woof ");
    }
  }
}

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

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(" What do pigs call again? ? Pig barking ");
    }
  }
}

Lao Wang next door came to realize the unison of all animals (down with the existence of such a character as Lao 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)
    {
      // All beasts sing in unison ( You can use reflection here to initialize all inheritance IAnimal All the animals, I won't write this, 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 1 Pass 
      for (int i = 0; i < animals.Count; i++)
      {
        animals[i].Shout();
      }

      
    }
  }
}

That's my rough view of this interface! Although the interface is very simple to use, 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 the interface.


Related articles: