【问题标题】:Share a method between 2 classes C#在 2 个类 C# 之间共享一个方法
【发布时间】:2015-12-16 07:05:35
【问题描述】:

我已经简化了我的内容:

class A : SomeClassICantAccess
{
    IVIClass ivi = new IVIClass();

    public A()
    {
        string address = "1111";
        ivi.Initialize(address);
    }

    public void SendCommand(string cmd)
    {
        ivi.Send(cmd);
    }
}

class B : SomeClassICantAccess
{
    IVIClass ivi = new IVIClass();

    public B()
    {
        string address = "2222";
        ivi.Initialize(address);
    }

    public void SendCommand(string cmd)
    {
        ivi.Send(cmd);
    }
}

class Main()
{
    //I want my main to keep this format
    A a = new A();
    B b = new B();

    a.SendCommand("commands");
    b.SendCommand("commands");
}

如您所见,AB 类具有相同的SendCommand() 方法,但由于它们的ivis 使用不同的地址初始化,因此将命令发送到不同的仪器。

在两个不同的类中复制粘贴相同的方法似乎是错误的。但我真的希望我的 Main() 看起来像现在的样子 - 以便清楚 SendCommand() 是指仪器 A 还是仪器 B。

如何合并它们?

【问题讨论】:

  • 你真的需要两节课吗?难道你不能有一个具有两个工厂方法的类来创建具有不同初始数据的实例吗?如果没有,您可能需要一个两者都派生自的抽象基类。
  • @Jon Skeet 合并两个类并在类中实例化两个不同的ivis?但是我仍然需要两组SendCommand() 方法——一组包含ivi1.Send(),另一组包含ivi2.Send()
  • 不,你只有一个类,但你会创建不同的类的instances。基本上与un-lucky给出的答案相同。
  • //我希望我的 main 保持这种格式 stackoverflow.com/a/34305991/985798
  • 我错过了代码中的继承,对此感到抱歉。 A 类和 B 类实际上继承自我无法访问的类(DLL 引用)。有了这个,我怎样才能仍然创建一个基类并让 A 和 B 继承它?

标签: c# class generics methods


【解决方案1】:

如果这是您的实际情况,则不需要两个类,您可以只处理A

A 的类定义:

class A()
{
    IVIClass ivi = new IVIClass();
    public A(string address)
    {        
        ivi.Initialize(address);
    }
    public void SendCommand(string cmd)
    {
        ivi.Send(cmd);
    }
}

如何使用:

class Main()
{   
    A a = new A("1111");//initialize  ivi with "1111"
    A b = new A("2222");//initialize  ivi with "2222"
    a.SendCommand("commands");
    b.SendCommand("commands");
}

【讨论】:

    【解决方案2】:

    您需要一个基本接口,我们称之为ICommandSender 和一个以 ICommandSender 实例作为源的扩展方法。

    // Base interface
    public interface ICommandSender
    {
        // Shared definition
        IVIClass ivi;
    }
    
    public class A : SomeOtherClass, ICommandSender
    {
        // A code here
    }
    
    public class B : SomeOtherClass, ICommandSender
    {
        // B code here
    }
    
    // Extension Methods are held in a static class (think Façade Pattern)
    public static class ExtMethods
    {
        // Accept the source and cmd 
        // The this keyword indicates the target type.
        public static void SendCommand(this ICommandSender source, string cmd)
        {
            source.ivi.Send(cmd);
        }
    }
    
    // Main doesn't change at all.
    class Main()
    {
        //I want my main to keep this format
        A a = new A();
        B b = new B();
    
        a.SendCommand("commands");
        b.SendCommand("commands");
    }
    

    【讨论】:

    • 抱歉,我在代码中遗漏了一些东西,A 类和 B 类已经从某个我无法访问的类继承(它来自程序集引用)。
    • 没关系。您可以使用一个类实现任意数量的接口。
    • 目前我有Class A : SomeOtherClass,我该如何实现ICommandSender?我继承了界面中的SomeOtherClass吗?
    • 您只需继承 SomeOtherClass 并实现 ICommandSender。我已经更新了 A 和 B 类定义的答案代码。
    【解决方案3】:

    引入基类?

    class BaseClass
    {
        IVIClass ivi = new IVIClass();
        public void SendCommand(string cmd)
        {
            ivi.Send(cmd);
        }
    }
    
    class A : BaseClass
    {   
        public A()
        {
            string address = "1111";
            ivi.Initialize(address);
        }
    }
    
    class B : BaseClass
    {   
        public B()
        {
            string address = "2222";
            ivi.Initialize(address);
        }
    }
    

    【讨论】:

    • 如果当前 A 类和 B 类已经从 C 类继承(而且我对 C 类无能为力),我该怎么做。
    【解决方案4】:
    abstract class AbstractClass{
    
       protected String socket;
    
       public AbstractClass(String socket){
          this.socket = socket;
       }
    
       public virtual void SendCommand(String cmd){
         //dostuff
       }
    
    }
    
    class A : AbstractClass{
      public A(String socket):base(socket){
      }
    
      //you can ovverride the virtual method if need be. 
    }
    

    B 类也一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多