さて、私は次のクラスを持っています:
- プレーヤー マネージャー (プレーヤーの追加、削除など)
- プレーヤー(あらゆる種類のことを行います)
- クライアント (データの送信、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.
}