C # Interface Usage,

Source: Internet
Author: User

C # Interface Usage,

1. Interface: an interface, like an abstract class, also represents a rule. Once this rule is used, relevant methods must be implemented. For the C # language, because it can only inherit from one parent class, it is better to use interfaces if multiple rules need to be implemented. 2. interface Definition interface name {method declaration;} 3. If multiple methods with the same name exist in different interfaces, you must explicitly specify the interface name, for example: 4. The use of interfaces can also achieve polymorphism. The Code is as follows: class Program {static void Main (string [] args) {// provides three classes, list of completed salary calculation <ICalu> list = new List <ICalu> (); list. add (new Boss (); list. add (new Clert (); list. add (new Clert (); list. add (new Customer (); foreach (ICalu emp in list) {emp. calu (); // polymorphism} class Boss: ICalu {public void Calu () {Console. writeLine ("Calculate boss salary") ;}} class Customer: ICalu {public void Calu () {Console. writeLine ("Calculate customer salary") ;}} class Clert: ICalu {public void Calu () {Console. writeLine ("Calculate employee salary") ;}} interface ICalu {void Calu () ;}5. interface Application Example 6. interface demousing System; using System. collections. generic; using System. linq; using System. text; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {List <IShow> list = new List <IShow> (); list. add (new Map (); list. add (new Voice (); list. add (new Video (); list. add (new ThreeD (); foreach (IShow ishow in list) {ishow. show () ;}} interface IShow {void Show ();} public class Map: IShow {public void Show () {Console. writeLine ("Show image") ;}} public class Voice: IShow {public void Show () {Console. writeLine ("Playing Sound") ;}} public class Video: IShow {public void Show () {Console. writeLine ("Show video") ;}} public class ThreeD: IShow {public void Show () {Console. writeLine ("3D interaction ");}}}

  

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.