2010-07-22 37 views
3

我可以繪製一個圓,但無法繪製矩形或繪製一條線。你們能看到我失蹤的是什麼嗎?類繼承和多態 - 繪製簡單形狀

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

回答

0

爲矩形你drawShape方法繪製一個橢圓。 此外,您的「劃清界限」的代碼是空白:

case 3: 
      // draw a line 
      break; 
0
  1. 你實現你的Circle類中的每一個形狀。爲什麼?頁面呈現有趣。你爲什麼在c.drawShaper(g)中繪製一個矩形?你把這個和你的Rectangle類混合在一起了嗎?
  2. 您正在使用r.drawShape(g)繪製橢圓。你應該打電話給r.drawShaper(g)。爲什麼你有兩種方法?
  3. 你還沒有實現畫一條線。
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); 
    } 
} 
+0

Franci,我清理了一下你的代碼。希望您對我所做的更改沒有任何問題,但請確認。 – 2010-07-22 23:33:56

+0

@Steven Sudit - 大聲笑,這不是我的代碼,我只是複製了OP代碼,並做了最小的修改,使其工作。儘管如此,您的更改使其更具可讀性,並且符合最常用的C#編碼約定,所以它實際上使答案更好。 – 2010-07-22 23:54:29

+0

好。只要你不冒犯。 – 2010-07-23 00:25:54

1

好吧,弗郎的答案是正確的,但有更多的這一點。

您需要一個靜態Shape,您可以將CircleSquare分配給。然後,你總是畫出這個Shape,它使用多態性來決定繪製什麼形狀。

多態性的要點在於你不需要case/select。