public class testConfig
{
private string m_IP;
private int m_Port;
private string m_UserName;
private string m_Password;
private bool m_IsOpen;
private int m_Size;
public string IP { get => m_IP; set => m_IP = value; }
public int Port { get => m_port; set => m_port = value; }
public string UserName { get => m_UserName; set => m_UserName = value; }
public string Password { get => m_Password; set => m_Password = value; }
public bool IsOpen { get => m_IsOpen; set => m_IsOpen = value; }
public int Size { get => m_Size; set => m_Size = value; }
}
public class testReadConfig:testconfig
{
public void readXML(string xmlFile)
{
string ip = "";
int port = 9986;
string username = "";
string password = "";
int size = 1024;
//读取配置文件内容
......
//如何赋值给testConfig中的属性
base.IP = ip;
base.Port = port;
base.UserName = username;
base.Password = password;
base.Size = size;
}
}
public class testHandle:testConfig
{
private Msk.Cntroller con;
public bool OpenCNT()
{
bool result = false;
if(con != null) Close();
//需要使用testConfig中的属性
con = new Msk.Cntroller();
con.IP = base.IP;
con.Port = base.Port;
con.UserName = base.UserName;
con.Password = base.Password;
if ( con.Open() == true )
result = true;
else
result = false;
base.IsOpen = result;
return result;
}
public void Close()
{
if(con != null)
{
con.Close();
con = null;
}
base.IsOpen = false;
}
public void Write(string text)
{
if(con != null)
con.Write(text);
}
}
public class testTelnet:testConfig
{
private Msk.Telnet telnet;
public bool Open()
{
bool result = false;
if(telnet != null) Close();
telnet = new Msk.Telnet();
telnet.Size = base.Size;
telnet.Open();
}
public void Close()
{
if(telnet != null)
{
telnet.Close();
telnet = null;
}
}
public void Write(string text)
{
if(telnet != null)
telnet.Write(text);
}
}
public class InfoHelper
{
private string m_IP;
private int m_Port;
private string m_UserName;
private string m_Password;
private bool m_IsOpen;
public string IP { get => m_IP; set => m_IP = value; }
public int Port { get => m_port; set => m_port = value; }
public string UserName { get => m_UserName; set => m_UserName = value; }
public string Password { get => m_Password; set => m_Password = value; }
public bool IsOpen { get => m_IsOpen; set => m_IsOpen = value; }
public bool readXML(string xmlFile);
public void CntrollerOpen();
public void CntrollerClose();
public void CntrollerWrite();
public bool TelnetOpen();
public void TelnetClose();
public void TelnetWrite();
}
public class test
{
static void Main(string[] args)
{
//实现目的,对外只暴露這一個類,而且這個類是可以實例化多個的,不是用static
InfoHelper info = new InfoHelper();
info.readXML("D:\\config.xml");
info.CntrollerOpen();
info.CntrollerWrite("1000");
info.CntrollerClose();
info.TelnetOpen();
info.TelnetClose("2000");
info.TelnetClose();
}
}
上面是偽代碼,想實現的效果
1、把這4個類( testConfig、testReadConfig、testHandle、testTelnet)的功能整個在一個類中 InfoHelper
2、對外訪問時,只需要調用 InfoHelper 這個類即可,如 test 類中的 Main 方法
注意:不能使用靜態方法,因為 InfoHelper 有可能實例化多個
可以继承多个接口,也可以用聚合来做,就是把testConfig、testReadConfig、testHandle、testTelnet放到InfoHelper里
把這4個類聚合到一個類中,是不是指:在1個類中實例會4個類(testConfig、testReadConfig、testHandle、testTelnet)
那 testReadConfig、testHandle、testTelnet 應該是不能訪問到同一個 testConfig 類吧
你可以InfoHelper继承testConfig,其他三个在InfoHelper中实例化对象并创建对应的方法进行调用。
class InfoHelper:testConfig { private testReadConfig _readConfig; private testHandle _handle; private testTelnet _telnet; public InfoHelper() { _readConfig = new testReadConfig(); _handle = new testHandle (); _telnet= new testTelnet (); } public void readXML(string xmlFile) { _readConfig.readXML(xmlFile); } public void OpenCNT() { _handle.OpenCNT(); } public void WriteHandle(string text) { _handle.Write(text); } public void WriteTelnet(string text) { _telnet.Write(text); } public void Open() { _telnet.Open(); } public void Close() { _handle.Close(); _telnet.Close(); } }
這3個類應該無法訪問 testConfig 中的屬性才對吧?
private testReadConfig _readConfig;
private testHandle _handle;
private testTelnet _telnet;