Solution to the problem of C # interface

Source: Internet
Author: User
Tags ming
This time the project is useful to the interface, the beginning is not a special understanding of the interface, just know the interface definition is very simple, and even feel that this interface is only superfluous (personal development time). Now start the team development, only to find that the interface is so important and convenient!

Next come to talk about my this time on the use of the interface of the superficial views, said to the hope that everyone praise, said the wrong place to hope that you have a lot of suggestions!

Ready go!

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

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

Then use an example to illustrate:

Company A decided to develop an animal system that contained many animals, and the company decided to make every animal scream.

Speaking of which, we are generally the various programmers to achieve their own animals to be implemented after the start of a drastic dry!!!

X Programmer implements Dog This class, he writes a yell method void Han () {...}

Y programmer implements cat This class, he writes a Yell method void shout () {...}

M programmer realizes pig this class, he writes a Yell method void shout (String content) {...}

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

Well, now all completed the respective needs to complete the animals, the next door to the old King began to realize the Beasts Qi Ming!!!! &¥%¥*%¥¥%¥ a foul language! How do you write this? To call each other???

To see, x Programmer English is not very good, also do not have too much to tube, just write the method that the animal shouts, y Programmer and M Programmer write yell Method name is same, but m programmer also pass animal SHOUT content!!!!!

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

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

X,y,m programmer inherited, X,m immediately found a problem, and then began to change their hands of the class

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

And then we'll post the code and 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 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:ianimal    {public        void shout ()        {            Console.WriteLine ("Wang Woo");    }}}

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

The old king next door came to realize the wild beasts of Qi Ming (down with the existence of the old king of this character)


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)        {            //Beasts Qi ( Here you can use reflection to initialize all the animals that inherit the ianimal, I don'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 once for            (int i = 0; i < animals. Count; i++)            {                animals[i]. Shout ();}}}    

My rough view of this interface is finished! Interface Although this thing is very simple to use, but we still need to understand the role of this interface, I hope that this article can let more novice like me to interface this thing take the first step

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.