Home >Backend Development >C#.Net Tutorial >C# Learning Diary 26---Interface Type

C# Learning Diary 26---Interface Type

黄舟
黄舟Original
2017-01-21 15:33:281855browse

An interface contains the definition of a set of related functions that a class or structure can implement. For example, using an interface can include behavior from multiple sources in a class. Since the C# language does not support multiple inheritance, multiple inheritance can be achieved through interfaces. In short, the interface only contains the declaration of members (properties, events, indexers). How the definition of a member is implemented is determined by its derived class.

Declare an interface:

The interface is declared using the interface keyword, which is similar to the declaration of a class. Interface declarations are public by default, and interfaces cannot contain constants, fields, operators, instance constructors, destructors, or types. Interface members automatically become public members and cannot contain any access modifiers. Members cannot be static members either. For example:

public interface person //Define an interface

{ void setname();} //Declare a method

An instance of an interface:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test1  
{//定义一个person接口  
    public interface person  
    {//只声明方法  
        void setname(string name);  
        void setsex(char sex);  
        void setage(uint age);  
        void getinformation();  
    }  
    //people继承于person  
    class people:person  
    {  
        private string name;  
        private char sex;  
        private uint age;  
        //实现接口中的方法  
       public void setname(string name)  
        { this.name = name; }  
       public void setsex(char sex)  
        { this.sex = sex; }  
       public void setage(uint age)  
        { this.age = age; }  
       public void getinformation()  
        {  
            Console.WriteLine("姓名:\t"+name);  
            Console.WriteLine("性别:\t"+sex);  
            Console.WriteLine("年龄:\t"+age);  
        }  
      
    }  
    //多重继承  
    class student :people,person  
    { }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            people peo = new people();  
            peo.setname("HC666");  
            peo.setsex('男');  
            peo.setage(18);  
            peo.getinformation();  
  
            student stu = new student();  
            stu.setname("HC555");  
            stu.setsex('男');  
            stu.setage(19);  
              
            stu.getinformation();  
        }  
    }  
}

Result:

C# Learning Diary 26---Interface Type

My interface seems to have no effect. If it is removed, it can still run.

If we look at it from a design perspective. There are several classes that need to be written in the project. Since these classes are relatively complex and the workload is relatively large, each class requires a staff member to write. For example, programmer A will define the Dog class, and programmer B will write the Cat class. These two classes have nothing to do with each other originally, but because the user needs them to both implement a method about "calling", this requires a constraint on them. Let them all inherit from the IBark interface, in order to facilitate unified management. In addition, One is the convenience of calling. Of course, the purpose can be achieved without using an interface. But in this case, the constraint is not so obvious. If there are such classes and Duck classes, etc., it is inevitable that someone will miss such a method. .So it is more reliable and more binding through the interface.

The above is the content of C# Learning Diary 26---interface type. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn