C # Learning Diary---Interface (interface) type

Source: Internet
Author: User
An interface contains a definition of a set of related functions that a class or struct can implement, for example, using an interface to include behaviors from multiple sources in a class. Because the C # language does not support multiple inheritance, multiple inheritance can be implemented through interfaces. In short, the interface contains only the declarations of members (properties, events, indexers). How the definition of a member is implemented is determined by his derived class.

Declare an interface:

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

public interface person//define an interface

{void SetName ();} Declaring a method

An instance of an interface:

Using System;  Using System.Collections.Generic;  Using System.Linq;    Using System.Text;          namespace Test1 {//define a person interface public interface The person {//only declares the method void SetName (string name);          void Setsex (char sex);          void Setage (uint age);      void GetInformation ();          }//people inherits from Person class People:person {private string name;          private char sex;          private UINT Age;         Implement the method in the interface 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 ("Name: \ T" +name);              Console.WriteLine ("Gender: \ T" +sex);          Console.WriteLine ("Age: \ T" +age); }}//Multiple inheritance class student:p Eople,person {} class program {static void Main ( String[] args)         {People peo = new people ();              Peo.setname ("HC666");              Peo.setsex (' Male ');              Peo.setage (18);                Peo.getinformation ();              Student stu = new Student ();              Stu.setname ("HC555");              Stu.setsex (' Male ');                            Stu.setage (19);          Stu.getinformation (); }      }  }

Results:

I do not seem to have any effect on this interface Oh, if you remove the same can run,

If we look at it from the point of view of design. A project with several classes to write, because these classes are more complex, the workload is relatively large, so that each class needs to occupy a staff to write. For example, a programmer to the dog class, b programmer to write cat class. The two classes are not connected. But because users need them to implement a "call" method. It's going to be a constraint on them. Let them all inherit from the Ibark interface, the purpose is to facilitate unified management. The other is convenient to call. Of course, you can do this without the use of an interface. But that is not so obvious. If such a class has duck class, and so on, it is inevitable that some people will miss out on this method. So it is more reliable through the interface, stronger binding.

The above is the C # Learning Diary---Interface (interface) type of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.