2014-02-23 14 views
0
namespace ShapesDemo 
{ 
    abstract class GeometricFigure 
    { 
     private int width, height; 

     public GeometricFigure(int width, int height) 
     { 
      this.width = width; 
      this.height = height; 
     } 

     public int Width 
     { 
      get { return width; } 
      set 
      { 
       width = value; 
       ComputeArea(); 
      } 
     } 

     public int Height 
     { 
      get { return height; } 
      set 
      { 
       height = value; 
       ComputeArea(); 
      } 
     } 

     public abstract int ComputeArea(); 
    } 
} 

class Rectangle : GeometricFigure 
{ 
     private int area; 

     public Rectangle(int width, int height) : base (width, height) 
     { 
     } 

     public override int ComputeArea() 
     { 
      area = Width * Height; 
      return area; 
     } 
    } 

    class Square : Rectangle 
    { 
     public Square(int width, int height) : base(width, width) 
     { 
     } 

     public Square(int side) : base (width,height) 
     { 
     } 

     // Here is the problem. There is a requirement to add a 
     // second constructor that uses one dimension for both width and height, 
     // but I am getting an error on any attempt I use. 
     // Any help would be appreciated. Thank you 
    } 
} 
+2

你爲什麼要允許'公共廣場(int width,int height)'?這與Square的含義完全相反。 – SLaks

+2

**錯誤說**是什麼? – SLaks

+1

你可能是指'公共廣場(int side):base(side,side)'。 –

回答

4

這一個不會編譯:

public Square(int side) : base (width,height) 
{ 
} 

使其

public Square(int side) : base (side, side) 
{ 
} 

此外,您可能希望限制基類構造函數的可見性。

//public GeometricFigure(int width, int height) ... 
protected GeometricFigure(int width, int height) ... 

而且你不應該提供沒有意義的public Square(int width, int height)

相關問題