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();
}
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();
}
相关文章推荐
- 通过实现IReportServerCredentials Interface接口,变相实现sql2008 reporting service的匿名登录
- 面向对象多态的实现(抽象类Abstract和接口Interface的用法区别)
- php通过interface实现简单多态
- USE Interface CallBack 通过接口实现回调
- 接上篇,通过接口实现多态,求三角形,矩形,圆周长面积
- java 中关于抽象类和接口的范例,通过多态中的向上转移实现
- 通过接口(interface)来实现定义回调函数
- 黑马程序员面试题面向对象二(多态,抽象类abstract,接口Interface,内部类,异常Exception,包package,String类,StringBuffer)
- #笔记#圣思园 JavaSE 第22讲——多态,接口interface |implements|| static | final
- 静态代理之通过实现接口来实现代理
- 3.第二单元任务五实训:通过程序设计几何图形接口(Shape),实现类矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显
- 条款34:区分接口继承和实现继承(Different between inheritance of interface and inheritance of implemenation)
- PHP实现飞信接口来通过网页免费发短信
- 接口 interface 接口的多继承 implements实现 抽象和接口的异同点
- Java通过接口实现匿名类的实例
- C#通过IConvertible接口来实现自定义类型转换和计算
- MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject
- 到底是使用abstract method还是interface在实现多态时
- 【匿名内部类】通过内部类匿名实现接口
- Static Nested Class 和 Inner Class的不同。Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?