如何從另一個類訪問變量而不重構它?C#從另一個類訪問變量而不重構類
級#1:
namespace Server.world
{
public class WorldData
{
private entitys.Player[] Players = new entitys.Player[Convert.ToInt16(ConfigurationManager.AppSettings["maxplayers"])];
public entitys.Player this[int i]
{
get { return Players[i]; }
set { Players[i] = value; }
}
}
}
類#2:構建worldData
類:
namespace Server
{
class StartUp
{
public Server.tcpserver.TcpServer ListenerTcp = new Server.tcpserver.TcpServer();
public world.WorldData WorldData = new world.WorldData();
/// <summary>
/// Server start function
/// </summary>
public void Start()
{
string rootFolder = ConfigurationManager.AppSettings["rootfolder"];
if (!Directory.Exists(rootFolder))
{
Directory.CreateDirectory(rootFolder);
string pathString = Path.Combine(rootFolder, "world");
Directory.CreateDirectory(pathString);
}
ListenerTcp.StartListening();
//No code below this point
}
}
}
類#3:
namespace Server.tcpserver
{
class TcpServer
{
int counter = 0;
public void StartListening()
{
IPAddress ipAddress = Dns.GetHostEntry("127.0.0.1").AddressList[0];
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
Server.client.Client Client = new Server.client.Client(clientSocket);
Console.WriteLine("Player Connected!");
//get world playerdata here
}
}
}
}
我會怎麼做呢?我看到處處都找不到它
你在哪裏實例化類3?你可以在構造函數中傳入你的'WorldData'對象。你可以在你的類中實例化它3 – paqogomez 2014-09-19 20:11:22
類「StartUp」和「Class3」是如何相互關聯的? – 2014-09-19 20:11:35
只有將該類轉換爲「靜態」,纔可以訪問成員而無需安裝 – 2014-09-19 20:11:38