2010-10-19 131 views
0

好吧,我正在製作一個具有非常長的路徑來獲取變量的類的包裝。舉例,它具有如下:如何訪問內部類中的外部類變量

Class1.Location.Point.X 
Class1.Location.Point.Y 
Class1.Location.Point.Z 
Class1.Location.Curve.get_EndPoint(0).X 
Class1.Location.Curve.get_EndPoint(0).Y 
Class1.Location.Curve.get_EndPoint(0).Z 
Class1.Location.Curve.get_EndPoint(1).X 
Class1.Location.Curve.get_EndPoint(1).Y 
Class1.Location.Curve.get_EndPoint(1).Z 

現在,在我的包裝,我願意把它簡化爲這樣:

Wrapper.X 
Wrapper.Y 
Wrapper.Z 
Wrapper.P0.X 
Wrapper.P0.Y 
Wrapper.P0.Z 
Wrapper.P1.X 
Wrapper.P1.Y 
Wrapper.P1.Z 

我的包裝看起來像這個:

public class Wrapper 
{ 
    protected Class1 c1 = null 
    public Wrapper(Class1 cc1) 
    { 
     c1 = cc1; 
    } 

    public int X 
    { 
      get{return C1.Location.Point.X;} 
    } 
    public int Y 
    { 
      get{return C1.Location.Point.Y;} 
    } 
    public int Z 
    { 
      get{return C1.Location.Point.Z;} 
    } 
} 

現在我的問題是P0.X和cie。我不知道該怎麼做。我嘗試了一個子類,但它不允許我訪問我的變量c1。我怎樣才能做到這一點?

+1

你的意思是'返回C1.Location.Curve.get_EndPoint(0).X;'不起作用? – 2010-10-19 19:19:31

+0

'Class1'對象類的代碼是什麼?因爲我沒有得到P0.X和cie的東西。否則,你的Wrapper類會暴露一個屬性,允許訪問你的c1字段嗎? – 2010-10-19 19:22:04

+0

@Frederic:是的,但我想通過寫Wrapper.P0.X來得到它們......但無論如何,我想通了,會發布答案 – Wildhorn 2010-10-19 19:26:14

回答

0

嗯,我想通了(似乎我需要在這裏發佈一個問題,以找出我自己的很多東西)。

是相當基本的東西,我不明白爲什麼我沒有弄清楚更快。

我做了一個子類Point0(1類C1),並添加到我的包裝的變量Point0名爲point0和一個名爲P0屬性返回point0,所以它給了我Wrapper.P0.X

0

兩個想法得到的東西類似於你正在尋找的東西。 你可以在包裝

實現一個索引屬性
class Wrapper{ 
    public int this[int index]{ 
    get{ return C1.Location.Curve.get_EndPoint(index); } 
    } 
} 

這將使用戶有類似的東西調用此:

Wrapper[0].X 

或者,如果你真的想有「P0」的性質和「P1」,你可以讓它們返回由get_EndPoint(int)返回的對象(如Frederic在他的評論中所建議的那樣)。

class Wrapper{ 
    public EndPoint P0{ 
    get{ return C1.Location.Curve.get_EndPoint(0); } 
    } 
} 
+0

是的,但是get_EndPoint中有更多的東西()並且只需要/想要X,Y,Z用於該包裝。這就是爲什麼。就像我說的,這個包裝的想法是簡化太複雜的迷宮課程。 – Wildhorn 2010-10-19 19:51:22