抽象类|基础内容|风格

抽象基类:Shape

public abstract class Shape
    {
        protected int xpos, ypos, height, width;
        protected Pen bPen;

        public Shape(int x, int y, int w, int h)
        {
            bPen = new Pen(Color.Black);        //using System.Drawing;
            xpos = x;
            ypos = y;
            height = h;
            width = w;
        }

        public abstract void draw(Graphics g);   //抽象类中至少存在一个抽象方法。没有花括号。实现代码在派生类中。

        public virtual float getArea()   //虚方法。可被基类调用。
        {
            return height * width;
        }

派生类:Rectangle

class Rectangle : Shape
    {
        public Rectangle(int x, int y, int w, int h)
            : base(x, y, w, h)
        {
            bPen = new Pen(Color.Red);
        }

        //对抽象类Shape中的抽象方法进行覆写。 在Rectangle类进行实例化之后进行调用。
        public override void draw(Graphics g)    
        {
            g.DrawRectangle(bPen, xpos, ypos, width, height);
        }  
    }


Rectangle的子类:Square

class Square : Rectangle                     //Square 继承自 Rectangle
    {
        public Square(int x, int y, int w)   //注意base的形式。
            : base(x, y, w, w)
        {
            bPen = new Pen(Color.Blue, 2);  // 定义一个新画笔,用来区分其它的类
        }   

        //这里没有draw方法。调用基类的draw方法。
        //也可以自己写一个新的draw方法(new) ,用来对基类的draw方法进行隐藏/覆写。 
        //public new void draw(Graphics g)
        //{ }
    }


Rectangle的另一个子类:DoubleRectangle

class DoubleRectangle : Rectangle
    {
        private Pen rdpen;                                                    //新声明一个Pen,draw方法调用的Pen是有区别的。
        public DoubleRectangle(int x, int y, int w, int h)
            : base(x, y, w, h)
        {
            rdpen = new Pen(Color.Green, 3);
        }


        public override void draw(Graphics g)
        {
            base.draw(g);                                                                            //基类的Pen
            g.DrawRectangle(rdpen, xpos + 5, ypos + 5, width, height);          //新声明的Pen
        }
    }



对以上类进行调用。<首先创建PictureBox一个。使用它的Paint动作>

public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            init();                                                   //初始化FormMain
        }

        Shape rect, squ, dourec;                         // 创建三个Shape类型


        private void init()
        {
            rect = new Rectangle(10, 20, 100, 100);             //对Shape类型分别进行“不同”的实例化。

            squ = new Square(120, 20, 100);

            dourec = new DoubleRectangle(10, 140, 100, 100);
        }


        private void pic_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            rect.draw(g);                  // 分别调用各自的draw方法
            squ.draw(g);
            dourec.draw(g); 
        }

    }


<小结>

1.抽象类(基类)中包含一个/多个完整的可工作的方法。但至少有一个为抽象方法。

2.抽象类不能被实例化。其派生类可以。

3.若抽象类的全部方法都是抽象方法的话,那么其本质上等于一个接口。但是它却有限制条件: 它不可以被继承也不能继承别人,而接口可以。


PS:第一次写,力求简单明了。才疏学浅,难免有错误。


下面是运行结果:




posted @ 2008-07-04 13:50  tommy&gina  阅读(299)  评论(0编辑  收藏  举报