现在的位置: 首页 > 综合 > 正文

Polynom-Interface通过接口实现多态

2013年09月15日 ⁄ 综合 ⁄ 共 2070字 ⁄ 字号 评论关闭

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();

        }

抱歉!评论已关闭.