"Go" interface for C # notes (Interface)

Source: Internet
Author: User

Text: The interface of small-base C # notes (Interface)

Compared with the various Daniel, I was just a new contact with the game development soon. But I can not always stay in the worship of their stage, only to continue to learn, to know, it is possible to be worshipped one day. Well, needless to talk nonsense, more than 10 years of school life Let me know, sometimes put their newly-learned knowledge to organize and share, can make the memory more profound.

today I'll tidy up the interface. For most new contact programming novice, should always have the idea that the interface is a useless thing (I also before). The sense of the interface is to declare the point attribute, there is a method name, no practical significance. Now I do not explain more, first to use the interface, perhaps after the use of a sudden feel that it is quite good! For example, we now do an FPS game in which players can choose from three classes: infantry, tanker, and healer. They all have their own exclusive skills USERPG (), Drivetank (), Treat (), and they all have generic skills walk (), Run (), Shoot (). OK, the front is the premise, now start to write code, first write interface iplayer. [Code]csharpcode:
Using system;using system.collections.generic;interface iplayer{    //is written here is a generic skill    void Walk (); Be sure to remember that these methods do not have modifiers such as public, protected    void Run ();    void Shoot ();}
Then we should write the class of the specific classes [Code]csharpcode:
Using system;using system.collections.generic;class Infantry:iplayer//The specific type of soldier to use this interface {    //The following three generic skills must be implemented in this class , less one will compile the error public    void Walk ()    {        //write the content of the specific Walk method        Console.WriteLine ("Infantry is walking");    }    public void Run ()    {        //write the contents of the specific Run method        Console.WriteLine ("Infantry is Running");    }    public void Shoot ()    {        //write the content of the specific Shoot method        Console.WriteLine ("Infantry is Shooting");    }    When the methods are implemented in the interface, we can write each professional's exclusive skills, but even if this is not written can be compiled through the OH. So if you don't write, you'll see the specifics of your project. Public    void Userpg ()    {        Console.WriteLine ("Infantry is userpging");}    }
Then the other two also wrote according to the first one [Code]csharpcode:
Using system;using system.collections.generic;class Tankman:iplayer//The specific type of soldier to use this interface {    //The following three generic skills must be implemented in this class, One less will compile the error public    void Walk ()    {        //write the content of the specific Walk method        Console.WriteLine ("Tankman is Walking");    }    public void Run ()    {        //write the contents of the specific Run method        Console.WriteLine ("Tankman is Running");    }    public void Shoot ()    {        //write the content of the specific Shoot method        Console.WriteLine ("Tankman is Shooting");    }    When the methods are implemented in the interface, we can write each professional's exclusive skills, but even if this is not written can be compiled through the OH. So if you don't write, you'll see the specifics of your project. Public    void Drivetank ()    {        Console.WriteLine ("Tankman is Drivetanking");}    }
[Code]csharpcode:
Using system;using system.collections.generic;class Medic:iplayer//The specific type of soldier to use this interface {    //The following three generic skills must be implemented in this class, One less will compile the error public    void Walk ()    {        //write the content of the specific Walk method        Console.WriteLine ("Medic is Walking");    }    public void Run ()    {        //write the contents of the specific Run method        Console.WriteLine ("Medic is Running");    }    public void Shoot ()    {        //write the content of the specific Shoot method        Console.WriteLine ("Medic is Shooting");    }    When the methods are implemented in the interface, we can write each professional's exclusive skills, but even if this is not written can be compiled through the OH. So if you don't write, you'll see the specifics of your project. Public    void Treat ()    {        Console.WriteLine ("Medic is Treating");}    }
Ok, the class of three classes is done, look at the three types of soldiers are different. 1. class name; 2. three generic skills; 3. Have their own unique skills. In a straightforward sense, these different places you can safely change to the content you want. Now let's use these three classes. We want three classes to run, and then they each use their own skills, here's the code: [Code]csharpcode:
Using system;using system.collections.generic;class war{    static void Main (string[] args)    {        // There are quite three players who have created three different classes of characters        infantry Player0 = new Infantry ();        Tankman player1 = new Tankman ();        Medic Player2 = new Medic ();        Then they all ran a road        Player0. Run ();        Player1. Run ();        Player2. Run ();        Player 0 uses RPG missiles, Player 1 drives tanks, Player 2 uses medical skills        Player0. USERPG ();        Player1. Drivetank ();        Player2. Treat ();        Console.readkey ();    }}
We're going to look at the results, not according to the process we're doing.

The above is the simple application of the interface, we will summarize the following:

Usage of the interface:

1. The interface is written in general things, for example, I am going to write "pig", "dog", "cat" and so on, I will write an "animal" interface, these animals can "run", "Jump", "called", then the "Animal" interface declared "Run", "Jump", "called" The three methods 2. All methods in the interface must be implemented in a specific class, that is to say "pig", "dog", "cat" must Have "run", "Jump", "call" method; 3. Specific classes can have a method that is not in the interface, such as "pig" there is an "arch cabbage" method, "dog" and "cat" do not have this method.

The role of the interface:

1. Unified management: No matter how many people write specific classes, as long as they all implement the same interface, they will know that the methods in the class are necessary;

2. Differences: The same is common approach, the difference is that the content of the method implementation can be different. Can also have a unique method;

3. Convenient to add new classes in the future: for example, in the future, but also to add an engineer, do not need to recall the class have any common method, the implementation interface can be.

OK, these are some of my understanding of interfaces. There may be some deviations, and I hope you can help me point it out. After the time, I was fine to do some other study notes.

"Go" interface for C # notes (Interface)

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.