Skip to main content
 首页 » 编程设计

c#之获得与在静态类中实现接口(interface)相同的功能的另一种方法是什么

2024年02月06日13落叶无声

我做了一些函数来控制硬件。但是,如果存在真实硬件(G.demomode = true),我还想调用实现相同接口(interface)的真实硬件函数。

您可以在下面看到我的尝试,但是 DVDDHW.setSupply(voltage); 行不太正确,即因为静态类无法实现接口(interface)。有更好的方法吗?

最终目标是为硬件工程师定义一个接口(interface)(也许这个词不合适),以便他可以在接口(interface)中指定未指定的功能。

我尝试进行尽职调查,发现了几个关于这个主题的话题。但是,我不知道如何为我的用例实现他们的替代解决方案。任何帮助或指示都会很棒。

谢谢!

public interface IPowerSupply 
{ 
    bool setSupply(double voltage); 
} 
 
public static class DVDDHW : IPowerSupply 
{ 
    public bool setSupply(double voltage) 
    { 
        i2c.write("DVDD ON"); //or something that involves turning the real hardware on 
        return true; 
    } 
} 
 
public class DVDD : IPowerSupply 
{ 
    public bool setSupply(double voltage) 
    { 
        DevLog.DevLog.addToLog(string.Format("Supply set: {0}V: ", voltage) + this.GetType().ToString()); 
        if (G.demoMode == false) //demoMode is false because HW is connected 
            { 
                DVDDHW.setSupply(voltage); //What is another way to accomplish this? 
            } 
        return true; 
    } 
} 
 
 
//Code to execute below: 
foreach PowerSupply ps in PowerSupplyList // List contains an instance of DVDD in this example 
{ 
    ps.setSupply(3.5); // Set each supply to 3.5V 
} 

请您参考如下方法:

如果您只想要实现接口(interface)的类的一个实例,那么单例是一种选择。

MS 操作方法是 https://msdn.microsoft.com/en-us/library/ff650316.aspx .

对于您的代码,以下将起作用:

public interface IPowerSupply 
{ 
    bool setSupply(double voltage); 
} 
 
public class DVDDHW : IPowerSupply 
{ 
    IPowerSupply _instance; 
 
    public static IPowerSupply Instance 
    { 
        get 
            { 
                if (_instance == null) 
                    _instance = new DVDDHW(); 
                return _instance; 
            } 
    } 
    private DVDDHW() { } 
 
    public bool setSupply(double voltage) 
    { 
        i2c.write("DVDD ON"); //or something that involves turning the real hardware on 
        return true; 
    } 
} 
 
public class DVDD : IPowerSupply 
{ 
    public bool setSupply(double voltage) 
    { 
        DevLog.DevLog.addToLog(string.Format("Supply set: {0}V: ", voltage) + this.GetType().ToString()); 
        if (G.demoMode == false) //demoMode is false because HW is connected 
            { 
                DVDDHW.setSupply(voltage); //What is another way to accomplish this? 
            } 
        return true; 
    } 
} 
 
 
//Code to execute below: 
foreach PowerSupply ps in PowerSupplyList // List contains an instance of DVDD in this example 
{ 
    ps.setSupply(3.5); // Set each supply to 3.5V 
} 

与静态类相比,单例的行为方式略有不同。如果您的应用程序是多线程的,或者如果您希望在首次访问该类型时在构造函数中有一些其他代码,那么这些很重要。如果这些事情对您来说没有任何意义,那么您不必担心 :)

编辑:

正如 Scott 所指出的,get 访问器中的代码是不必要的。一个简化版本是这样的:

public class DVDDHW : IPowerSupply 
{ 
    static readonly IPowerSupply _instance = DVDDHW(); 
 
    public static IPowerSupply Instance 
    { 
        get { return _instance; } 
    } 
 
    private DVDDHW() { } 
 
    public bool setSupply(double voltage) 
    { 
        i2c.write("DVDD ON"); //or something that involves turning the real hardware on 
        return true; 
    } 
} 

此外,这段代码有一个拼写错误(已修复):

//Code to execute below: 
foreach IPowerSupply ps in PowerSupplyList // List contains an instance of DVDD in this example 
{ 
    ps.setSupply(3.5); // Set each supply to 3.5V 
}