. Net interface learning notes,. net learning notes

Source: Internet
Author: User

. Net interface learning notes,. net learning notes
1. interface declaration

The declaration of an interface cannot contain: data member or static variable. It can only contain the declaration of the following types of static member functions: method, attribute, event, and indexer. The Declaration cannot contain any implemented code. After each member becomes famous, the semicolon must be used.

The interface declaration can have any modifier public, protect, internl, or private.

The interface members are implicitly public and do not allow any access modifiers, including public.

1 Public Interface IMyInterface2 {3 // The Interface member is implicitly public and only declares 4 int Method1 (int nVar1, int nVar2); 5}
2. Interface implementation

Only the class and struct can implement the interface, and the interface name must be included in the base class list. The class or structure used by the interface must be implemented for each member of the interface.

1 Class MyClass: IMyInterface2 {3 // when implicitly implementing interface members, the access modifier must be set to public 4 public int Method (int a, int B) 5 {6 Console. writeLine ("{0}", a + B); 7 return a + B; 8} 9}

If a class inherits the base class and implements the interface, the base class name in the base class list of the class must be placed before all interfaces. A class can only have one base class. All types listed must be interface names.

An interface is not only a list of members to be implemented by a class or structure. It is a reference type and cannot be accessed directly through a member of a class object, however, we can forcibly convert the Class Object Reference to the interface type to obtain the reference pointing to the interface. In this way, we can use the point number to call the interface method. We recommend that you use as for conversion.

1 Static void Main (string [] args) 2 {3 int a = 5; 4 int B = 8; 5 MyClass mc = new MyClass (); 6 // call the implementation method of Class Object 7 mc. method (a, B); 8 // convert the type to interface type 9 IMyInterface mif = mc as IMyInterface; 10 mif. method (a, B); 11}

Output result: 13

13

The advantage of using as for forced conversion: if the class implements the interface, it will return the interface reference (SAFE). If the class does not implement the interface, it will throw an exception to facilitate timely error discovery.

3. implement multiple interfaces

Class or structure can implement any number of interfaces.

All Implemented interfaces must be listed in the base class list and separated by commas.

If two interfaces implemented by a class have duplicate members (with the same signature and the same return type), when the class implements interface members, once implemented, all interfaces that contain duplicate members can be satisfied.

1 interface interface1 {void Method (int a, int B);} 2 interface interface2 {void Method (int a, int B );} 3 // implement multiple interfaces separated by commas (,). 4 public class MyClass: interface1, interface2 5 {6/* 7 * If the return types of the two interface member functions are the same as those of the parameters, 8 * You can perform one implementation of 9 */10 public void Method (int a, int B) 11 {12 Console. writeLine ("{0}", a + B); 13} 14}

If you want to deliberately detach the interface members of these two interfaces, you need to create an explicit interface member implementation. Format: interface name + dot separator + interface member function. Note: When the interface member is displayed, the default value is public, and no access modifier can be added.

1 interface interface1 {void Method (int a, int B);} 2 interface interface2 {void Method (int a, int B );} 3 // implement multiple interfaces separated by commas (,). 4 public class MyClass: interface1, interface2 5 {6 // display the interface member implementing interface1 7 // The default value is public, the access modifier 8 void interface1.Method (int a, int B) 9 {10 Console cannot be added. writeLine ("{0}", a + B); 11} 12 void interface2.Method (int a, int B) 13 {14 Console. writeLine ("{0}", a + B); 15} 16}

But in this way, only the implementation of explicit interface members is available, and there is no class-level implementation. Therefore, this method cannot be clicked after the object of this class is instantiated. If an explicit interface member is implemented, class-level implementation is allowed, but not required. In addition, explicit interface members can be intelligently accessed by pointing to interface references, even member functions in the class to access interface members.

If a class implements an interface, its subclass also inherits the interface members implemented by its base class and does not need to be implemented again.

4. interface inheritance

To specify an interface to inherit other interfaces, you should put the interface name after the interface name in the interface declaration in the form of a comma-separated list. The inheritance format is the same as that of the class. However, unlike classes, an interface can inherit any number of interfaces and carry out multiple inheritance. In addition to the self-declared interfaces, a sub-interface also includes all interface members of all parent interfaces.

5. code Demo 1 namespace ConsoleApplication2 2 {3 interface ILoveWorld {void SayHello ();} 4 class Creature {} 5 6 // when both the base class and interface are implemented, the base class should be placed at the beginning of the 7 class Person: Creature, ILoveWorld 8 {9 // implicit declaration interface, which can be accessed by class objects, it can also be converted to an interface reference object to access 10 void SayHello () 11 {12 Console. writeLine ("Hello World! "); 13} 14 // explicitly declare an interface member. It can only be converted to an interface reference object to access 15 void ILoveWorld. sayHello () 16 {17 Console. writeLine ("Hello World! "); 18} 19} 20 class Cat: Creature, ILoveWorld21 {22 // compared with the explicit interface implementation method. Class-level interface implementation is not required. 23 void ILoveWorld. sayHello () 24 {25 Console. writeLine ("MiaoMiaoMiao"); 26} 27} 28 class Dog: Creature, ILoveWorld29 {30 // public 31 void ILoveWorld cannot be used when explicitly calling interface members. sayHello () 32 {33 Console. writeLine ("WangWangWang"); 34} 35} 36 class Program37 {38 static void Main (string [] args) 39 {40 // declare a Creature array 41 Creature [] pCreatureArray = new Creature [3]; 42 // The relationship between the last learned object-oriented IS-A is used here 43 pCreatureArray [0] = new Person (); 44 pCreatureArray [1] = new Cat (); 45 pCreatureArray [2] = new Dog (); 46 foreach (var creature in pCreatureArray) 47 {48 // use as to convert 49 ILoveWorld B = creature as ILoveWorld; 50 B. sayHello (); 51} 52 Console. readKey (); 53} 54} 55}Interface Demo

Running result:

  

 

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.