类继承和多态 - 绘制简单的形状

时间:2010-07-22 22:52:37

标签: c# drawing polymorphism system.drawing

我可画一个圆圈,但无法画一个矩形或画一条线。你们能看到我失踪的是什么吗?

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Shapes
{
    public abstract class Shapes
    {
        // these shapes are defined with four coordinates
        protected int x1, y1, x2, y2;

        // this method initializes the coordinates
        public void setCoordinates(int x1, int y1, int x2, int y2)
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        // abstract method to draw the shape
        public abstract void drawShape(Graphics g);
        public abstract void drawShapes(Graphics g);

    } // end of Shapes

    // class Circle derives from Shapes
    public class Circle : Shapes
    {
        // no argument constructor
        public Circle()
        {
            setCoordinates(0, 0, 0, 0);
        }

        // constructor with four arguments
        public Circle(int a, int b, int w, int h)
        {
            setCoordinates(a, b, w, h);
        }

        public override void drawShape(Graphics g)
        {
            g.DrawEllipse(new Pen(Color.Green), x1, y1, x2, y2);
        }

        public override void drawShaper(Graphics q)
        {
            q.DrawRectangle(new Pen(Color.Green), x1, y1, x2, y2);
        }
    }

    public class Rectangle : Shapes
    {
        // no argument constructor
        public Rectangle()
        {
            setCoordinates(0, 0, 0, 0);
        }

        // constructor with four arguments
        public Rectangle(int a, int b, int w, int h)
        {
            setCoordinates(a, b, w, h);
        }

        public override void drawShape(Graphics g)
        {
            g.DrawEllipse(new Pen(Color.Green), x1, y1, x2, y2);
        }

        public override void drawShaper(Graphics q)
        {
            q.DrawRectangle(new Pen(Color.Green), x1, y1, x2, y2);
        }
    }

    // tests Shapes hierarchy
    public class TestShapes : Form
    {
        private static Circle c;
        private static int shape;

        private static Rectangle r;

        public static void Main()
        {
            // Here you can ask the user to enter the type and 
            // the coordinates for a shape
            shape = 1;
            c = new Circle(100, 100, 50, 50);

            r = new Rectangle(100, 100, 50, 50);

            // starts the application and makes the form visible
            Application.Run(new TestShapes());
        }
        // this method executes automatically and paints the form
        protected override void OnPaint(PaintEventArgs e)
        {
            // Get Graphics Object
            Graphics g = e.Graphics;

            // Draw text on the form
            g.DrawString("Testing C# inheritance!", new Font("Arial", 18),
new SolidBrush(Color.Blue), 5, 5);
            switch (shape)
            {
                case 1:
                    // draw a circle
                    c.drawShape(g);
                    break;

                case 2: 
                    r.drawShape(g);
                    // draw a rectangle
                    break;

                case 3:
                    // draw a line
                    break;
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

像这样改变你的课程:

public abstract class Shape
{
    // ...
    // abstract method to draw the shape
    public abstract void DrawShape(Graphics g);
} // end of Shape

// class Circle derives from Shape
public class Circle : Shape
{
    // ...
    public override void DrawShape(Graphics g)
    {
        g.DrawEllipse(new Pen(Color.Green), x1, y1, x2, y2);
    }
}

public class Rectangle : Shape
{
    // ...
    public override void DrawShape(Graphics g)
    {
        g.DrawRectangle(new Pen(Color.Green), x1, y1, x2, y2);
    }
}

答案 1 :(得分:1)

好的,弗朗西的回答是正确的,但还有更多。

您需要一个静态Shape,您可以指定CircleSquare。然后,您总是绘制Shape,它使用多态来决定要绘制的形状。

多态性的全部意义在于你不需要那种情况/选择。

答案 2 :(得分:0)

Rectangle的 drawShape 方法绘制一个椭圆。 此外,您的“画线”代码为空白:

case 3:
            // draw a line
            break;

答案 3 :(得分:0)

  1. 您正在实现Circle类中的每个形状。为什么?页面呈现出来很有趣。为什么要在c.drawShaper(g)中绘制一个矩形?你把它和你的Rectangle类混在一起吗?
  2. 您正在使用r.drawShape(g)绘制椭圆。你应该调用r.drawShaper(g)。为什么你有这两种方法?
  3. 您尚未实现划线。