Talk a little bit about C # interface problems novice quick to watch _c# tutorial

Source: Internet
Author: User
Tags ming

This time the project is useful to the interface, the beginning is not particularly understanding the interface, but simply know the interface definition is very simple, even feel that this interface is only superfluous (personal development time). Now start team development, only to find that the interface is so important and convenient!

Next, I would like to talk about the use of the interface of the superficial insights, said to the hope that everyone praise, said the wrong place hope that we have a lot to forgive suggestions!

READY go!

The definition of the interface is not much said, it has a very important point of knowledge, is that all inherited this interface class must implement the definition of the interface, speaking of this must, in the team development, as long as we agreed that the interface, then our code is NOT unified!!!

This is the 1th that I think the interface is important: it is easy for us to unify the provisions of the project, easy to manage the team code!

Let's use one more example to illustrate:

A company decided to develop an animal system, which contains a lot of animals, the company decided to achieve the shouting behavior of each animal ...
Speaking of which, we are generally the various programmers get their own to achieve the animal class after the start of the bold!!!
X Programmer implements Dog This class, he writes a shouting method void Han () {...}
Y programmer implements the cat class, he writes a Yell method void shout () {...}
M programmer implements pig This class he writes a Yell method void shout (String content) {...}
..................

Well, now that they have completed their respective needs of the animals, the next door to the old King began to realize the Beast QI Ming!!!! &¥%¥*%¥¥%¥ a foul language burst! How do you write that? To call each other???

To see, x Programmer English is not very good, and do not have too much to pipe, but to write the method of animal shouting, Y programmers and M programmers write shouting method name is the same, but M programmers also have to pass the contents of animal shouting!!!!!

The Old king next door is now going to have to call all the animals. One animal to call the method ...

OK, next meeting to discuss, the old king next door to define an animal interface, all animal classes have to inherit this interface, this interface defines only a void shout (); (Not too much writing, secretly lazy)

X,y,m programmer inherited, X,m immediately found that there are problems, and then began to change their hands of the class

Then the old king began to come to the Beasts Qi Ming! hahaha haha

And then post the code, and everybody look.

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 yell
    ///</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:i Animal
  {public
    void shout ()
    {
      Console.WriteLine ("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");
}}

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:i Animal
  {public
    void shout ()
    {
      Console.WriteLine () "What did the pig call it??" Pig called ");}}


The old king next door to realize the Animals Qi Ming (down old Wang such a person'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)
    {
      //Beast Qi Ming ( Here you can use reflection to initialize all the animals that inherit ianimal, I will not 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 again for
      (int i = 0; i < animals. Count; i++)
      {
        animals[i]. Shout ();}}}


I'm finished with a rough view of the interface! Interface this thing is easy to use, but we still have to understand the role of this interface, I hope that this article can let more like me as beginners to the interface this thing take the first step.

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.