Interface Definition and usage

Source: Internet
Author: User
The Interface Definition starts with an uppercase letter I. The method only defines its name. in C #, the method is a public method by default. The public modification method is not allowed; otherwise, a compilation error occurs. The interface can be inherited from other interfaces, if multiple interfaces are inherited, the parent interface list is separated by commas.
Interfaces can be implemented through classes. When the base list of classes contains both the base classes and interfaces, the base classes appear in the list first. Classes must implement their abstract methods;
Interface Usage: see Code (Transfer)

Use interface (instance 1)

Using system;
Namespace dage. Interface

{
// Printer interface
Public interface iPrint
{
String
Returnprintname ();
}
}
//--------------------------------------------
Using
System;
Using dage. interface;
Namespace
Dage. Print
{
// HP printer
Public class HP: iPrint
{
Public
String returnprintname ()
{
Return "this is an HP printer ";

}
}
}
//--------------------------------------------
Using
System;
Namespace dage. Print
{
// EPS printer type
Public class EPS:
IPrint
{
Public String returnprintname ()
{
Return
"This is an EPS printer ";

}
}
}
//--------------------------------------------
Using
System;
Using dage. interface;
Namespace dage
{
// Print
Public
Class printer
{
Public printer ()
{}
Public String
Printname (iPrint)
{
Return iPrint. returnprintname ();

}
}
}
//--------------------------------------------
-- Winfrom:
Private
Void button#click (Object sender, system. eventargs E)
{
Printer P = new
Printer ();
Switch (this. combobox1.text)
{
Case "HP ":

MessageBox. Show (P. printname (new HP ()));
Break;
Case "EPS ":

MessageBox. Show (P. printname (New EPS ()));
Break;
Default:

MessageBox. Show ("This brand is not found! ");
Break;
}
}

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.