package com.stephen.javatest02;
public class MachineConfig {
private static MachineConfig a = null;
private String ServerIP="";
private int ServerPort=0;
private MachineConfig()
{
}
public String getServerIp()
{
return this.ServerIP;
}
public int getServerPort()
{
return this.ServerPort;
}
public synchronized void setServerParam(String ServerIP, int ServerPort)
{
this.ServerIP = ServerIP;
this.ServerPort = ServerPort;
}
public static synchronized MachineConfig getInstance()
{
if(null == a)
a = new MachineConfig();
return a;
}
}
调用方法:
MachineConfig mc = MachineConfig.getInstance();
mc.setServerParam("172.16.24.19", 8080);
System.out.println("server ip:" + mc.getServerIp());
System.out.println("server port:" + mc.getServerPort());
MachineConfig mc2 = MachineConfig.getInstance();
mc2.setServerParam("192.168.1.1", 6981);
System.out.println("server ip:" + mc.getServerIp());
System.out.println("server port:" + mc.getServerPort());
运行结果:
server ip:172.16.24.19
server port:8080
server ip:192.168.1.1
server port:6981
public class MachineConfig {
private static MachineConfig a = null;
private String ServerIP="";
private int ServerPort=0;
private MachineConfig()
{
}
public String getServerIp()
{
return this.ServerIP;
}
public int getServerPort()
{
return this.ServerPort;
}
public synchronized void setServerParam(String ServerIP, int ServerPort)
{
this.ServerIP = ServerIP;
this.ServerPort = ServerPort;
}
public static synchronized MachineConfig getInstance()
{
if(null == a)
a = new MachineConfig();
return a;
}
}
调用方法:
MachineConfig mc = MachineConfig.getInstance();
mc.setServerParam("172.16.24.19", 8080);
System.out.println("server ip:" + mc.getServerIp());
System.out.println("server port:" + mc.getServerPort());
MachineConfig mc2 = MachineConfig.getInstance();
mc2.setServerParam("192.168.1.1", 6981);
System.out.println("server ip:" + mc.getServerIp());
System.out.println("server port:" + mc.getServerPort());
运行结果:
server ip:172.16.24.19
server port:8080
server ip:192.168.1.1
server port:6981