开发者

Class inheritance and polymorphism - drawing simple shapes

开发者 https://www.devze.com 2023-01-08 17:42 出处:网络
I can draw a circle, but am unable to draw a rectangle or draw a line.Can you guys see what it is that I am missing?

I can draw a circle, but am unable to draw a rectangle or draw a line. Can you guys see what it is that I am missing?

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 : Shap开发者_开发知识库es
    {
        // 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;
            }
        }
    }
}


Change your classes like this:

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


Ok, Franci's answer is correct, but there's more to this.

You need a static Shape that you either assign a Circle or Square to. Then, you always draw that Shape, which uses polymorphism to decide what shape to draw.

The whole point of polymorphism is that you don't need that case/select.


Your drawShape method for Rectangle draws an ellipse. Also, your "draw a line" code is blank:

case 3:
            // draw a line
            break;


  1. You are implementing every shape within your Circle class. Why? The page rendered funny. Why are you drawing a rectangle in c.drawShaper(g)? Did you mix this up with your Rectangle class?
  2. You are drawing an ellipse with r.drawShape(g). You should call r.drawShaper(g). Why do you have both methods?
  3. You haven't implemented draw a line.
0

精彩评论

暂无评论...
验证码 换一张
取 消