您的位置:首页 > 其它

Polynom-Interface通过接口实现多态

2009-11-07 12:11 387 查看
Polynom-Interface通过接口实现多态


static void Main()




...{


IGElement ge1= new CLine();


IGElement ge2= new CRectangle();


ge1.ID = 1;


ge2.ID = 11;


Console.WriteLine("ID:{0},{1}", ge1.ID, ge2.ID);


ge1.Draw();


ge2.Draw();


}




using System;




namespace Polinom




...{




/**//// <summary>


/// CGElement 的摘要说明。


/// </summary>


public interface IGElement




...{


int ID




...{


get;


set;


}


void Draw();


}


}




using System;


using System.Drawing;




namespace Polinom




...{




/**//// <summary>


/// CLine 的摘要说明。


/// </summary>


///




public class CLine:IGElement




...{


private int m_ID;


private PointF m_Begin;


private PointF m_End;




public int ID




...{




get...{return m_ID;}




set...{m_ID =value;}


}




public CLine()




...{


}




public void Draw()




...{


Console.WriteLine("绘直线段。");


}




}


}




using System;


using System.Drawing;




namespace Polinom




...{




/**//// <summary>


/// CRectangle 的摘要说明。


/// </summary>


///


public class CRectangle:IGElement




...{


private int m_ID;


private PointF m_LT;


private PointF m_RB;




public int ID




...{




get...{return m_ID;}




set...{m_ID = value;}


}




//矩形左上角、右下角属性定义


public void Draw()




...{


Console.WriteLine("绘矩形。");


}


}


}



这个调用例程更清楚


static void Main()




...{


IGElement ge1= new CLine();


IGElement ge2= new CRectangle();


IGElement Elem;




ge1.ID = 1;


ge2.ID = 11;


Console.WriteLine("ID:{0},{1}", ge1.ID, ge2.ID);


ge1.Draw();


ge2.Draw();




Elem = ge1;


Elem.ID = 10;


Console.WriteLine("ID:{0}", Elem.ID);


Elem.Draw();




Elem = ge2;


Elem.ID = 100;


Console.WriteLine("ID:{0}", Elem.ID);


Elem.Draw();




}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐