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;
}
}