2011-05-04 172 views
2

我有一個領域的基礎類:重寫屬性屬性

class Base 
{ 
    public int A; 
    public int ShowA() 
    { 
     Console.WriteLine(A); 
    } 
} 

我有一個派生類:

class Derived : Base 
{ 
    public Derived() 
    { 
     A = 5; 
    } 
} 

我使用GUI編輯器,顯示了類的所有領域。因爲Derived在其構造函數中設置了A,所以我不希望它在編輯器中顯示。編輯器不會輸入具有[HideInInspector()]屬性。
我該如何讓DerivedA屬性具有[HideInInspector()]屬性,而Base不會? 我不能使用關鍵字new,因爲我希望Base仍然在其功能中使用與Derived相同的字段(例如,(new Derived()).ShowA()將打印5)。

編輯:它看起來有技巧屬性和new將無法​​正常工作,因爲GUI編輯器對待new兩次(一次爲基地,一次爲派生)的字段。

回答

3

試試這個

class Base 
{ 
    public virtual int A {get; set;} 
    public int ShowA() 
    { 
     Console.WriteLine(A); 
    } 
} 

class Derived : Base 
{ 
    public Derived() 
    { 
     A = 5; 
    } 

    [HideInInspector()] 
    public override int A 
    { 
     get { return base.A;} 
     set { base.A = value} 
    } 
} 
+0

我能請你問一下'HideInInspector()'不? – 2011-05-04 08:26:13

+0

OP特別指出他們不能使用'new' ... – Jon 2011-05-04 08:26:51

+0

@Uw在這個問題中,我說的GUI編輯器不會顯示'HideInInspector()'(不是Visual Studio)的字段。 – Dani 2011-05-04 08:28:49