C #-Interface

Source: Internet
Author: User
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

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.