首页新闻找找看学习计划

c# 只能继承一个类,这种方式如何处理

0
悬赏园豆:10 [待解决问题]
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 有可能實例化多個

大家有什麽思路嗎,我知道可以繼承多個接口,但我不知道如何實現,有大神幫忙改下嗎,謝謝了

dllnetspy的主页 dllnetspy | 菜鸟二级 | 园豆:202
提问于:2019-08-06 09:19
< >
分享
所有回答(2)
1

可以继承多个接口,也可以用聚合来做,就是把testConfig、testReadConfig、testHandle、testTelnet放到InfoHelper里

会长 | 园豆:5328 (大侠五级) | 2019-08-06 09:27

把這4個類聚合到一個類中,是不是指:在1個類中實例會4個類(testConfig、testReadConfig、testHandle、testTelnet)

那 testReadConfig、testHandle、testTelnet 應該是不能訪問到同一個 testConfig 類吧

支持(0) 反对(0) dllnetspy | 园豆:202 (菜鸟二级) | 2019-08-06 09:49
0

你可以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();
    }
}
HotSky | 园豆:237 (菜鸟二级) | 2019-08-06 15:57

這3個類應該無法訪問 testConfig 中的屬性才對吧?
private testReadConfig _readConfig;
private testHandle _handle;
private testTelnet _telnet;

支持(0) 反对(0) dllnetspy | 园豆:202 (菜鸟二级) | 2019-08-06 18:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册