0

さて、私は次のクラスを持っています:

  • プレーヤー マネージャー (プレーヤーの追加、削除など)
  • プレーヤー(あらゆる種類のことを行います)
  • クライアント (データの送信、recv などのネットワーク アクションが含まれます)。

Player マネージャーには Player クラスの配列があり、Client クラスは Player の構成であり、ユーザーが Client インターフェイスを見る必要がないため、プライベート アクセスが可能です。

リストの代わりに固定長の配列を使用したいという問題を除いて、すべてうまくいきます。クライアント クラスは実行時に決定されます。プレーヤーを初期化する場合は、プロパティに直接設定するか、セッター メソッドを使用して設定する必要があります。これにより、クライアント コンポジションをパブリックにする必要があります。

Player クラスのコンストラクターで Client プロパティを設定できるため、リストはこの問題で完全に機能しますが、より高速であるため、固定長の配列を使用することをお勧めします。クライアントを非公開にして、Player Manager クラスから設定するための回避策はありますか?

public class PlayerManager
{
    public Player[] players { get; set; }

    public PlayerManager(int maxplayers)
    {
        players = new Player[maxplayers];
    }

    public void Add(Client client)
    {
        Player player = FindPlayerSlot();
        player.client = client; //cant do this, client is private property
    }

}

public class Player
{
     private Client client { get; set; } //Need to hide this from user

     //I can set a constructor here for set the client property, but this would
     //force me to use a list.
}
4

2 に答える 2

1

internalプロパティにしてみてください。internalキーワードは、型またはメンバーが定義されているアセンブリの外部で非表示にします。

これは、これがすべて単一のアセンブリにあり、ユーザーがアセンブリを参照して使用することを前提としています。内部を表示する必要があるアセンブリが複数ある場合は、 を使用[assembly: InternalsVisibleTo("MyOtherAssembly")]して、内部としてマークされたメンバーにアクセスできる他のアセンブリのみを定義できます。

さらに、List<Client>サイズ変更時を除いて、固定配列よりも遅くなることはありません。List コンストラクタを初期容量で使用すると、固定配列と同じパフォーマンスが得られます。

于 2013-01-19T08:38:42.560 に答える
0

なぜさせないのですか

FindPlayerSlot(); // return a int type ?

コンストラクタを介してクライアントプロパティを設定しますか?

int playerid = FindPlayerSlot();
if (playerid > -1)
{
    Player player = new Player(client);
}

コンストラクターの追加

public class Player { プライベート Client client { get; 設定; } //これをユーザーから隠す必要があります

 //I can set a constructor here for set the client property, but this would
 //force me to use a list.
public Player(Client client)
{
    this.client = client;
}

}

于 2013-01-19T08:38:27.023 に答える