我正在写一些我希望在我的一些应用程序中使用的泛型类。例如,这个套接字类:
public class jSocket
{
public class Server
{
public static _Settings Settings = new _Settings();
public class _Settings
{
private int _Listen;
public int Listen
{
get
{
return _Listen;
}
set
{
_Listen = value;
}
}
private int _Port;
public int Port
{
get
{
if (_Port == 0)
_Port = 1337;
return _Port;
}
set
{
_Port = value;
}
}
private IPEndPoint _localEndPoint;
public IPEndPoint localEndPoint
{
get
{
if(_localEndPoint == null)
_localEndPoint = new IPEndPoint(Tools.getLocalIPAddress(), Port);
return _localEndPoint;
}
set
{
_localEndPoint = value;
}
}
}
public void Start()
{
//Do work
}
}
现在在我的应用程序中我实例化了这个类:
jSocket.Server newServ = new jComp.jSocket.Server();
但为什么我无法访问设置属性?我想做的是:
newServ.Settings.Port = 1001;
我是否需要为每个字段编写构造函数?
答案 0 :(得分:0)
Setting
字段为static
,您需要删除static
并将其设为实例字段。然后它将属于您的实例,您将能够访问它。< / p>
public _Settings Settings = new _Settings();