C # basic syntax Learning (9 ),

Source: Internet
Author: User

C # basic syntax Learning (9 ),

Interfaces in C #

In C #, an interface is a set of common methods or attributes. Interfaces can be inherited by other interfaces or classes, but cannot be instantiated.

1. attributes and methods contained in interfaces are public, not inherited or private. In fact, when defining interface members in C #, the accessibility of the specified interface members is not allowed to be displayed, but is automatically set to public by default.

2. interfaces can only contain common methods or attributes, but cannot contain other content, such as constructors and variables.

3. When an interface is inherited by a class, it is generally said that the class implements this interface, but less that the class inherits the interface.

4. The method and attribute in the interface only have the signature part, but not the implementation part. Even the braces after the interface name cannot exist. Otherwise, a compilation error occurs.

Define an interface with the interface keyword in C #

Access modifier interface Name

{

// Interface member (method and attribute)

}

In C #, the interface name starts with the letter I. The following code defines an interface IPerson

1 public interface IPerson 2 {3 void eat (object food); // method of the interface 4 void speak (string text ); // Interface Method 5 6 string name // interface attribute 7 {8 get; 9 set; 10} 11}

As mentioned above, variables and constructors cannot be defined in an interface, and the accessibility of methods or attributes in a specified interface cannot be displayed.

 

Interfaces can be inherited by classes or other interfaces. The syntax of the class inheritance interface is the same as that of the class inheritance class. The class name is followed by a colon and the interface name to be inherited, as shown below:

Access modifier class Name: Interface Name

{

// Class and interface member

}

The difference between a class inheritance interface and a class inheritance class is that a class can inherit multiple interfaces at the same time, but can only inherit from one class. When a class inherits multiple interfaces, multiple interfaces are separated by commas, as shown below:

Access modifier class Name: [base class name] [, interface 1] [, interface 2] [...] [, interface n]

{

// Class and interface member

}

The syntax of the interface inheritance interface is similar to that of the class inheritance interface, but the keyword interface is used when the interface is defined.

  Access modifier interface Name: [interface 1] [, interface 2] [, interface 3] [...] [, interface n]

{

// Interface member

}

  If a class inherits from an interface, the class must implement all methods and attributes defined in the interface. Because the interface defines the signature of methods and attributes, and the specific implementation code of these methods and attributes is written in the class inherited from the interface, when a class inherits from the interface, generally, a class implements an interface.

The implementation mentioned here has two meanings: first, the class inherits from the interface, and second, the class uses code to implement the methods and attributes defined in the interface.

If a class implements an interface, because the class inherits the self-interface, the class can be implicitly converted to an interface, which is the same as the implicit conversion from a derived class to a base class. If a class implements multiple interfaces, the class can be implicitly converted to any of them.

1 public interface IPerson 2 {3 void eat (object food); // method of the interface 4 void speak (string text ); // Interface Method 5 6 string name // interface attributes 7 {8 get; 9 set; 10} 11} 12 13 public class Student: IPerson14 {15 16 public void eat (object food) 17 {18 Console. writeLine (name + "eat:" + food. toString (); 19} 20 21 public void speak (string text) 22 {23 Console. writeLine (name + "say:" + text); 24} 25 26 private string _ name; 27 public string name28 {29 get30 {31 return _ name; 32} 33 set34 {35 _ name = value; 36} 37} 38} 39 static void Main (string [] args) 40 {41 IPerson person; 42 Console. writeLine ("Main: calling methods through interfaces"); 43 person = new Student (); 44 person. name = "Nick"; 45 person. eat ("apple"); 46 person. speak ("Hello"); 47 Console. writeLine ("Main: calling methods by class"); 48 Student s = new Student (); 49 s. name = "Jessice"; 50 s. eat ("rice"); 51 s. speak ("Hehe"); 52 53 Console. readLine (); 54}

Result

Main: Call the method Nick eat: appleNick say: HelloMain: Call ice eat: riceJessice say: Hehe through a class

 

  Implementation of explicit Interfaces

When an explicit interface is used, the method name used to implement the interface must be prefixed with the interface name.

1 public class NewStudent: IPerson 2 {3 4 void IPerson. eat (object food) 5 {6 Console. writeLine (_ name + "eat:" + food. toString (); 7} 8 9 void IPerson. speak (string text) 10 {11 Console. writeLine (_ name + "say:" + text); 12} 13 14 private string _ name; 15 string IPerson. name16 {17 get18 {19 return _ name; 20} 21 set22 {23 _ name = value; 24} 25} 26} 27 static void Main (string [] args) 28 {29 IPerson pers On; 30 Console. writeLine ("Main: calling methods through interfaces"); 31 person = new Student (); 32 person. name = "Nick"; 33 person. eat ("apple"); 34 person. speak ("Hello"); 35 Console. writeLine ("Main: calling methods through classes"); 36 Student s = new Student (); 37 s. name = "Jessice"; 38 s. eat ("rice"); 39 s. speak ("Hehe"); 40 41 Console. writeLine ("Explicit interface Example"); 42 person = new NewStudent (); 43 person. name = "Jason"; 44 person. eat ("Bread"); 45 person. speak ("Good Luck! "); 46 47 NewStudent ns = new NewStudent (); 48 // ns. name = "Lucy"; // an error is reported. The explicit interface can only be used to call the 49 50 Console. readLine (); 51}

Running result

Main: Call the method Nick eat: appleNick say: HelloMain: Call the method using the class named ice eat: riceJessice say: Hehe explicit interface Example Jason eat: BreadJason say: Good Luck!

When an explicit interface is implemented, the method (or attribute) names corresponding to the IPerson interface in the NewStudent class have the IPerson prefix and the method (or attribute) access modifiers such as public and protected are not allowed. When a class explicitly implements an interface, the method used to implement the interface in the class can only be implemented through the interface

But not through the instance of the interface.

 

  Comparison between interfaces and abstract classes

Interfaces are similar to abstract classes. Both of them can define a set of attributes and methods. They cannot be created and can only be used as the base classes of other classes. However, interfaces are very different from abstract classes, as shown below:

1. variables can be defined in abstract classes, but not in interfaces.

2. constructor can be defined in abstract classes, but not in interfaces.

3. Non-public members can be defined in abstract classes, such as protected, private, and internal-level method variables. Only public members can be defined in interfaces.

4. Non-abstract methods in abstract classes can have method bodies, while methods in interfaces can only be defined and cannot be implemented.

5. Since class C # can only inherit from a single class, once a class inherits from an abstract class, it cannot inherit from other abstract classes. That is to say, the inheritance of abstract classes is unique and exclusive. This problem does not exist in inheritance from interfaces. A class inherits an interface and does not affect the class's inheritance of other interfaces.

In summary, only public access-level method signatures and attribute signatures can be defined in the interface. abstract classes have the same behavior as common classes except for class generation, all members that can be defined in common classes can be defined in abstract classes. The methods in the abstract class allow the definition of the implementation code, so that you can

Put the public code in the derived class in the abstract class to reuse the code and reduce the amount of encoding in the derived class.

Examples of interfaces and abstract classes are as follows:

1 interface IPerson 2 {3 void eat (object food); // method of the interface 4 void speak (string text ); // Interface Method 5 6 string name // interface attribute 7 {8 get; 9 set; 10} 11} 12 13 public abstract class Person 14 {15 public void eat (object food) 16 {17 Console. writeLine (name + "eat:" + food. toString (); 18} 19 20 public void speak (string text) 21 {22 Console. writeLine (name + "say:" + text); 23} 24 25 private string _ name; 26 public string name 27 {28 get {return _ name ;} 29 set {_ name = value;} 30} 31} 32 33 public class Child1: IPerson 34 {35 36 public void eat (object food) 37 {38 Console. writeLine (name + "eat:" + food. toString (); 39} 40 41 public void speak (string text) 42 {43 Console. writeLine (name + "say:" + text); 44} 45 46 private string _ name; 47 public string name 48 {49 get 50 {51 return _ name; 52} 53 set 54 {55 _ name = value; 56} 57} 58 59 public void Study () 60 {61 Console. writeLine (name + "study hard ..... "); 62} 63} 64 65 public class Adult1: IPerson 66 {67 public void eat (object food) 68 {69 Console. writeLine (name + "eat:" + food. toString (); 70} 71 72 public void speak (string text) 73 {74 Console. writeLine (name + "say:" + text); 75} 76 77 private string _ name; 78 public string name 79 {80 get 81 {82 return _ name; 83} 84 set 85 {86 _ name = value; 87} 88} 89 90 public void Work () 91 {92 Console. writeLine (name + "work hard .... "); 93} 94} 95 96 public class Child2: Person 97 {98 public void Study () 99 {100 Console. writeLine (name + "study hard .... "); 101} 102} 103 104 public class Adult2: Person105 {106 public void Work () 107 {108 Console. writeLine (name + "work hard... "); 109} 110} 111 112 static void Main (string [] args) 113 {114 Child1 child1 = new Child1 (); 115 Child2 child2 = new Child2 (); 116 Adult1 adult1 = new Adult1 (); 117 Adult2 adult2 = new Adult2 (); 118 119 child1.name = "Jack"; 120 child1.eat ("apple "); 121 child1.speak ("hello"); 122 child1.Study (); 123 124 adult1.name = "Nick"; 125 adult1.eat ("apple"); 126 adult1.speak ("hello "); 127 adult1.Work (); 128 129 child2.name = "Lucy"; 130 child2.eat ("rice"); 131 child2.speak ("hello"); 132 child2.Study (); 133 134 adult2.name = "Lily"; 135 adult2.eat ("banana"); 136 adult2.speak ("hello"); 137 adult2.Work (); 138}

Running result

Jack eat: appleJack say: helloJack study hard.....Nick eat: appleNick say: helloNick work hard....Lucy eat: riceLucy say: helloLucy study hard....Lily eat: bananaLily say: helloLily work hard...

From the implementation code of the Child1 and Adult1 classes, both classes inherit from IPerson. To implement the methods defined in the interface, the same code is written twice in the two classes.

From the Code of the Person, Child2, and Adult2 classes, we can see that methods and attributes are implemented in the Person class, and there is no need to repeatedly implement them in the derived class, implementing code reuse.

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.