Programming C #. Interfaces

Source: Internet
Author: User

When a class implements an interface, it must implement all parts of the interface (methods and properties, etc.), and the effect is equivalent to the class stating: "I agree to fulfill the contract defined by this interface." ”

Inheriting from the abstract class implements the "Is-a (is a)" relationship, a "implement (Implementation)" relationship when implementing an interface, the difference being:

For example, a car is a means of delivery that can achieve Canbeboughtwithabigloan (which can be bought with a huge amount of money) (like a house)
1 /*************************************************************************************2  * 3 * 1. If a document class is defined, the class can be stored and compressed, so both the istorable and Icompressible interfaces are implemented4 * 2. Extension interface is to use a xinjiekou extension of the original interface, through the expansion of the interface we expressed the meaning:5 * Anything that implements a new interface must also implement the original interface6 * 3. The combination interface is the combination of existing interfaces, and can add new methods or properties to create a new interface7  * 8  * 9  * Ten  *  One  *  A  *  -  *  -  *  the  *  -  *************************************************************************************/ - usingSystem; -  + namespaceSimpleinterface - { +     //First Interface A     Interfaceistorable at     { -         //There is no access modifier in the method declaration of the interface, which is implicitly public because the interface is a contract to be used by other classes -         voidRead (); -         voidWrite (Object obj); -         //the declaration of a property does not implement the Get and set methods, but declares both methods -         intStatus {Get;Set; } in     } -  to     //Second Interface +     Interfaceicompressible -     { the         voidCompress (); *         voiddecompress (); $     }Panax Notoginseng  -     //Expansion Interface the     Interfaceiloggedcompressible:icompressible +     { A         //new interface adds a new method to record the number of bytes saved the         voidlogsavedbytes (); +     } -  $     //Combination Interface $     Interfaceistorablecompressible:istorable, Iloggedcompressible -     { -         //Store the size of the document before compression the         voidlogoriginalsize (); -     }Wuyi  the      Public classdocument:istorablecompressible -     { Wu         //data that stores the status property of the Istorable -         Private intStatus =0; About  $          PublicDocument (strings) -         { -Console.WriteLine ("Creating Document with:{0}", s); -         } A  +         //implementing the Read method the          Public voidRead () -         { $Console.WriteLine ("implementing the Read Method for istorable"); the         } the  the         //implementing the Write Method the          Public voidWrite (Object obj) -         { inConsole.WriteLine ("implemeting the Write Method for istorable"); the         } the         //Implementing Properties About          Public intStatus the         { the             Get the             { +                 returnstatus; -             } the             Set Bayi             { theStatus =value; the             } -         } -  the         //Implement Icompressible the          Public voidCompress () the         { theConsole.WriteLine ("Implementing Compress"); -         } the          Public voidDecompress () the         { theConsole.WriteLine ("Implementing Decompress");94         } the  the         //Implement Iloggedcompressible the          Public voidlogsavedbytes ()98         { AboutConsole.WriteLine ("Implementing Logsavedbytes"); -         }101 102         //Implement Istorablecompressible103          Public voidlogoriginalsize ()104         { theConsole.WriteLine ("Implementing Logoriginalsize");106         }107     }108 109     //Test Interface the      Public classTester111     { the         Static voidMain ()113         { theDocument doc =NewDocument ("Test document!"); the  the             //Convert document to various interfaces for Operation117Istorable Isdoc = doc asistorable;118             if(Isdoc! =NULL)119             { - Isdoc.read ();121             }122             Else123             {124Console.WriteLine ("Istorable not supported"); the             }126 127Iloggedcompressible Ilcdoc = doc asiloggedcompressible; -             if(Ilcdoc! =NULL)129             { the                 //Iloggedcompressible can call the method of the Icompress interface because it extends the interface131 ilcdoc.compress (); the ilcdoc.logsavedbytes ();133             }134             Else135             {136Console.WriteLine ("Iloggedcompressible not supported");137             }138 139 Console.readkey (); $ 141         }142     }143}
View Code

// Convert document to various interfaces for Operation             as istorable;

This means that if you are not sure whether a class implements a particular interface, you can use the AS operator to convert, and then test whether the structure of the transformation is null, so that the risk of causing the exception is not assumed to have been successfully converted. Of course it can be written like this:

Istorable Isdoc = (istorable) doc;

Interfaces compared to abstract classes:

Because C # does not support multiple inheritance, the interface is better. But

1. Aircraft will fly, birds will fly, they all inherit the same interface "Fly"; but F22 belongs to the abstract class of airplanes, the pigeons belong to the abstract class of birds.
2. Like iron doors are doors (abstract class), you want a door I can't give (can't instantiate), but I can give you a concrete iron gate or wooden door (polymorphic); and only the door, you can't say it is a window (single inheritance); A door can have a lock (interface) can also have a doorbell (multi-implementation). The door (abstract class) defines what you are, and the interface (lock) specifies what you can do (an interface is best to do only one thing, and you cannot ask for a lock to make a sound (interface pollution)).

Programming C #. Interfaces

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.