C # interface

Source: Internet
Author: User
Tags access properties

Transferred from: http://www.cnblogs.com/ArmyShen/archive/2012/09/04/2669809.html

An interface is an abstraction of a method, whereas an object is an abstraction of a class

The interface rules:

(1) Interface (interface) defines a contract that can be implemented by classes and structs

(2) An interface can contain methods, properties, events, and indexers

(3) All interfaces implicitly have public access properties

(4) An interface must not contain constants, fields, operators, constructors, destructors, types

(5) The interface cannot contain abstract, public, protected, internal, private, virtual, override, static modifiers

(6) The interface does not provide a specific implementation of the members it defines

(7) If a class inherits an interface, the class must implement all the methods in the interface, including the base interface of the interface

(8) interface can achieve multiple inheritance

1. Declare an interface

The interface keyword defines an interface, usually named with the i + Name interface interface imyinterface{void fun     ();}    

2. The interface has the same name as its base class method

Interfaceimyfirstinterface{voidFunc ();}//Second inherited the firstInterfaceimysecondinterface:imyfirstinterface{//In this case, if you have a method with the same name in second, you need to add the New keywordnew void Func ();} 
//Class A inherits the second interface, so it also implements the method second a:imysecondinterface{
// Because second and the method in the first interface have the same name, the method with the same name in first will be overwritten, which is actually the method in second void func () {}}public Span style= "color: #0000ff;" >class test{static Void Main () {A A = new A () ; A.func (); }}

3. Display interface member implementation (resolves 2 occurrences of the same name overlay problem)

Interfaceimyfirstinterface{voidFunc ();}Interfaceimysecondinterface:imyfirstinterface{NewvoidFunc ();}Classa:imysecondinterface{//using fully qualified interface member names as identifiers, you can resolve the problem of overriding the same name of the inherited interface, at which point the implemented interface is not a member of the class, it is only an implementation of the interface method, so you cannot use public to decorate its access rights
Implementation is a method in the first interfacevoidImyfirstinterface.func () {Console.WriteLine ("first); //implements second method void Second ");}} public class test{ Span style= "color: #0000ff;" >static void Main () {A A =     new A ();
//, when invoking a method, cannot directly use the object of the class to invoke the interface method that it implements, but only the interface name to be displayed, which confirms that these methods are not part of the class itself ( Imyfirstinterface) a). Func (); ((Imysecondinterface) a). Func (); }}

4. Multiple inheritance between interfaces

Interfaceia{voidF ();}Interfaceib:ia{NewvoidF ();}Interfaceic:ia{voidG ();}Interfaceid:ib,ic{}Classmyclass:id{//class inherits the ID interface, so the method in the interface (IB,IC) to which the ID inherits is implementedPublicvoidF () {Console.WriteLine ("Ib. F ()"); }PublicvoidG () {}}PublicClasstest{ static void Main () {MyClass c = new MyClass (); c.f ();  call the F () method in IB, because the ID interface inherited by the class inherits from the IB Interface ((IA) c). F (); /// call the F () method in IB, the ID first looks for the F () method in the interface IB to which it inherits, although there is a method with the same name in IA, at which point the method with the same name in IA is overwritten, that is, the path of IB to IA is hidden ((IB) c). F (); // call the F () method in IB, which is the implementation of the method in IB ((IC) c). F (); //Call the F () method in IB, a visual hiding rule for multiple inheritance: If a member is hidden in any of the access paths, it is hidden in all access paths ((ID) c). F (); // call the F () method in IB because the ID inherits the method in Ib }}

C # interface

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.