2012-10-26 85 views
1

我試圖得到以下結構:自定義類框架/結構

Game.Player.Position.setPos(X,Y,Z) 

這是什麼框架我到目前爲止有:

public partial class Game 
{ 
    public class Player 
    { 
     private class Position 
     { 
      public float setPos(float X, float Y, float Z) 
      { 
       //this code not included 
      } 
     } 
    } 
} 

這不正是給我我想要的。我想獲得如下:

Player player1 = new Player(); 
player1.Position.setPos(player1_X, player1_Y, player1_Z); 

但是,每當我宣佈player1-我得到的唯一選擇本民族的(player1.Equals,player1.GetHashCode,player1.ToString,ECT)不是player1.Position.setPos()或其他。

我不知道這是什麼called-所以如果有人知道我在哪裏可以得到更多這方面的信息,這也將有助於一堆。

感謝,

回答

4

你宣佈你的Position類OK,這是描述的位置是什麼。什麼你缺少的是對你Player類的屬性 - 說,一個Player可以有一個位置,即:

public partial class Game 
{ 
    public class Player 
    { 
     public class PositionClass 
     { 
      public float setPos(float X, float Y, float Z) 
      { 
       //this code not included 
      } 
     } 

     public PositionClass Position {get; set;} 

     public Player() { 
      Position = new PositionClass(); 
     } 
    } 
} 

另一件事是,你需要有Position類作爲Public,爲你的財產是要暴露在Player課程的範圍之外。

最後,您需要在構造函數中實例化您的Position屬性,以便您可以在聲明新的Player之後直接調用setPos,正如您已經聲明的那樣。

注:我已經承擔了VS2010財產申報爲簡潔。另外,我改變了你Position類被稱爲PositionClass,只是讓你可以看到什麼是什麼的例子 - 你通常不會這麼做,但你要因此他們不是太混亂看你namings。

+0

@ArpyClarkson不要忘記創建位置屬性;) – Silvermind

+0

@Silvermind:參見構造。 –

+0

對不起,我評論時一定錯過了。 – Silvermind