如何从另一个类访问变量而不重构它?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