1. Interface Definition Public interface IAttribute
{
String Name {get; set ;}
}
Public class Component: IAttribute
{
Public string Name
{
Get
{
Return "Zhang San ";
}
Set
{
This. Name = value;
}
}
}
2 interface implementation Public interface IPlayer
{
String GetName ();
String Show ();
}
Public class Options
{
Public static readonly string JIANDAO = "Scissors ";
Public static readonly string SHITOU = "Stone ";
Public static readonly string BU = "cloth ";
}
Public class Grandpa: IPlayer
{
Public string GetName ()
{
Return "grandpa ";
}
Public string Show ()
{
Random random = new Random ();
Int I = (int) (random. Next () * 1000) % 3;
Switch (I)
{
Case 0: return Options. JIANDAO;
Case 1: return Options. SHITOU;
Default: return Options. BU;
}
}
}
Public class Grandson: IPlayer
{
Public string GetName ()
{
Return "grandson ";
}
Public string Show ()
{
Return Options. JIANDAO;
}
}
3. interface inheritance
An interface can be inherited from one or more basic interfaces.
Interface IA {}
Interface IB: IA {}
Interface IC: IA, IB {}
Interface ID: IA, IB, IC {}
4. interfaces and callbacks
Static void Main (string [] args)
{
// Create a controller object and pass in the callback object provided to it
Resolve resolve = new Resolve (new PlayBasketball ());
Resolve. Play ();
Resolve = new Resolve (new PlayFootball ());
Resolve. Play ();
}
Public interface IPlayer
{
Void Play ();
}
Public class PlayBasketball: IPlayer
{
Public void Play ()
{
Console. WriteLine ("playing basketball ");
}
}
Public class PlayFootball: IPlayer
{
Public void Play ()
{
Console. WriteLine ("playing football ");
}
}
/// Control role -- controller object
/// </Summary>
Public class Resolve
{
// Hold an API reference and initialize it through the constructor
Private IPlayer player;
Public Resolve (IPlayer player)
{
This. player = player;
}
Public void Play ()
{
Player. Play ();
}
}
From: http://www.cnblogs.com/beniao/archive/2008/07/28/1249031.html