2017-02-20 35 views
0

我知道這個問題已被問到,但所有解決方案都沒有解決我的問題。我有繼承一個類的問題,它告訴我缺少哪些是我不知道的任何想法?非常感謝!!不包含採用0參數的約束器

這是我的代碼!

public class Rectangle : Shape (shape is an other file which is work) 
{ 
    private Pen pen = new Pen(Color.Blue); 
    private int size; 

    protected Rectangle(Point position, int size) : base(position) 
    { 
     this.size = size; 
    } 
     public Rectangle(int x, int y, int size) : base(new Point(x, y)) 
    { 
     this.size = size; 
    } 
    override public void Draw(Graphics r) 
    { 
     r.DrawRectangle(pen, 600, 100 , 50, 50); 

    } 
} 

public class Square : Rectangle (here it does not work when inherit) 
{ 
    override public void Draw(Graphics r) 
    { 
     r.DrawRectangle(pen, 300, 100, 50, 50); 
    } 

} 

回答

1

Rectangle有兩個構造函數。如果你打算從Rectangle繼承,你的能夠調用其中一個構造函數。您Square有,你有它各自的構造,能夠調入base(Point position, int size)base(int x, int y, int size)

+0

解決了!許多許多感謝所有! – JEERY

2

您需要定義Square一個構造函數,將調用基Rectangle

public class Square : Rectangle (here it dose not work when inherit) 
{ 
    public Square(Point position, int size) : base(position, size) 
    { 
    } 

    override public void Draw(Graphics r) 
    { 
     r.DrawRectangle(pen, 300, 100, 50, 50); 
    } 

} 

: base(position, size)碼。因爲你繼承,你也繼承Rectangle構造所以要麼你可以做我所做的事情和參數傳遞到基礎,也可以使Square參但定義你的價值觀:: base(new Point(200,200), 200)

+0

解決!許多人感謝所有 – JEERY