public class Base
{
// Protected to ensure that only the derived class can access the _panel attribute
protected Panel _panel;
public Base(Panel panel1)
{
_panel = panel1;
}
}
public class Derived : Base
{
public Derived() : base(new Panel())
{
// refer this way: base.panel
}
}
另外,如果你想只提供一個獲取,而不是一組派生類可以這樣做:
public class Base
{
// Protected to ensure that only the derived class can access the _panel attribute
private Panel _panel;
public Base(Panel panel1)
{
_panel = panel1;
}
protected Panel Panel
{ get { return _panel; } }
}
public class Derived : Base
{
public Derived() : base(new Panel())
{
// refer this way: base.Panel (can only get)
}
}
來源
2012-12-26 10:57:11
Ngm
好的。我實際上不能修改'Base'! – ispiro
@ispiro注意私人_panel字段實際上有點危險。如果您在寫「如何在此處使用panel1」時記住面板的其他用途,則應該這樣做,然後刪除專用字段(當然還有分配)。並感謝您清理代碼格式! – phoog
這正是我所做的(沒有私人領域)。但是,你的意思是「危險的」? – ispiro