2009-12-21 76 views
21

我有一個「米」類。 「米」的一個屬性是另一類叫做「生產」的類。 我需要訪問從生產類的電錶級(額定功率)屬性參考。在Meter的實例化中,powerRating是未知的。如何訪問C中的父對象#

我該怎麼做?

在此先感謝

public class Meter 
{ 
    private int _powerRating = 0; 
    private Production _production; 

    public Meter() 
    { 
     _production = new Production(); 
    } 
} 
+0

可能重複[什麼是從嵌套類訪問封閉類中的字段的最佳方式?](http://stackoverflow.com/questions/185124/whats-the-best-way-of-accessing-field -in-the-encl-class-from-the-nested-cl) – 2016-05-27 18:29:46

+2

@JimFell這根本不涉及嵌套類 – Rob 2016-05-28 04:43:15

回答

28

存儲爲生產成員到儀表實例的引用:

public class Production { 
    //The other members, properties etc... 
    private Meter m; 

    Production(Meter m) { 
    this.m = m; 
    } 
} 

然後在儀表類:

public class Meter 
{ 
    private int _powerRating = 0; 
    private Production _production; 

    public Meter() 
    { 
     _production = new Production(this); 
    } 
} 

另外請注意,您需要實現的存取方法/屬性,以便生產類可以實際訪問Meter類的powerRating成員。

+0

如果這兩個類在不同的組件中,並且包含流量計的組件有一個VS2010項目引用到包含生產的組件? – Snowy 2011-06-12 03:20:22

+1

@Snowy:因爲循環引用是不可能的,所以你需要重新安排你的實現並將一個類移動到另一個程序集(或者複製類,如果沒有其他幫助的話)。 – Christian 2011-06-14 05:53:21

9

您將需要一個屬性添加到您的生產類,並將其設置在其父點背,這並不存在默認情況下。

-3

是這樣的:

public int PowerRating 
    { 
     get { return base.PowerRating; } // if power inherits from meter... 
    } 
+0

這是父/子關係,而不是「繼承」關係。 – GalacticCowboy 2009-12-21 13:50:27

+0

@GalacticCowboy,哦,是的,我明白你的意思。傻我 – 2009-12-21 14:02:31

2

爲什麼不改變Production構造,讓你在建造時的基準傳:

public class Meter 
{ 
    private int _powerRating = 0; 
    private Production _production; 

    public Meter() 
    { 
     _production = new Production(this); 
    } 
} 

Production構造函數,你可以指定這一個私人領域或財產。然後Production將始終有權訪問父項。

0

你也許可以在Production對象中添加一個名爲'SetPowerRating(int)'的方法,它在Production中設置一個屬性,然後在使用Production對象中的屬性之前在Meter對象中調用該方法?

+0

我認爲這違反了KISS;必須記得在調用屬性之前調用一個函數使得它過於複雜的IMO。 – 2009-12-21 14:21:46

+0

的確,上述設計聽起來好像需要整體重新思考。我剛剛提供了一個解決方案,它可能不是最好的。 – 2009-12-24 13:20:54

26

我不會直接在子對象中引用父對象。在我看來,孩子們不應該對父母有任何瞭解。這會限制靈活性!

我會解決這個事件/處理程序。

public class Meter 
{ 
    private int _powerRating = 0; 
    private Production _production; 

    public Meter() 
    { 
     _production = new Production(); 
     _production.OnRequestPowerRating += new Func<int>(delegate { return _powerRating; }); 
     _production.DoSomething(); 
    } 
} 

public class Production 
{ 
    protected int RequestPowerRating() 
    { 
     if (OnRequestPowerRating == null) 
      throw new Exception("OnRequestPowerRating handler is not assigned"); 

     return OnRequestPowerRating(); 
    } 

    public void DoSomething() 
    { 
     int powerRating = RequestPowerRating(); 
     Debug.WriteLine("The parents powerrating is :" + powerRating); 

    } 

    public Func<int> OnRequestPowerRating; 
} 

在這種情況下,我使用FUNC <>通用的解決了這個問題,但可以用「正常」的功能來完成。 這就是爲什麼孩子(生產)完全獨立於其父母(米)。


但是!如果有太多的事件/處理程序,或者你只是想傳遞一個父對象,我會用一個接口解決它:

public interface IMeter 
{ 
    int PowerRating { get; } 
} 

public class Meter : IMeter 
{ 
    private int _powerRating = 0; 
    private Production _production; 

    public Meter() 
    { 
     _production = new Production(this); 
     _production.DoSomething(); 
    } 

    public int PowerRating { get { return _powerRating; } } 
} 

public class Production 
{ 
    private IMeter _meter; 

    public Production(IMeter meter) 
    { 
     _meter = meter; 
    } 

    public void DoSomething() 
    { 
     Debug.WriteLine("The parents powerrating is :" + _meter.PowerRating); 
    } 
} 

這看起來幾乎是相同的解決方案中提到,但接口可以定義在另一個程序集中,可以由一個以上的類實現。


Regards, Jeroen van Langen。