简体   繁体   English

2接口(i1,i2),2个抽象类(A:i1,B:i2),一个类(摘要:i1,i2)。 如何访问抽象类的所有方法

[英]2 Interface(i1, i2), 2 abstract class(A:i1, B:i2), one class(Summary :i1,i2). How to access all methods of abstract class

I have two abstract Class IExcelHelper and ICoreExcelHelper inherit by ExcelHelper and CoreExcelHelper. 我有两个抽象的类IExcelHelper和ICoreExcelHelper由ExcelHelper和CoreExcelHelper继承。 I have one class that inherits two interfaces: IExcelHelper & ICoreExcelHelper. 我有一个继承两个接口的类:IExcelHelper和ICoreExcelHelper。 I want to access all method of abstract class whether methods are not implemented. 我想访问抽象类的所有方法是否未实现方法。 Here I am unable to access the Test Method of ExcelHelper. 在这里,我无法访问ExcelHelper的测试方法。

any suggestion? 有什么建议吗?

public interface IExcelHelper
{
    void GenerateReport();
}
public interface ICoreExcelHelper
{
    void GenerateReport();
}

public abstract class ExcelHelper : IExcelHelper
{
    public abstract void GenerateReport();

    public void Test()
    {
        Console.WriteLine("Test");
    }
}

public abstract class CoreExcelHelper : ICoreExcelHelper
{
    public abstract void GenerateReport();
}

public class GenerateExcelStrategySummary : IExcelHelper, ICoreExcelHelper
{
    void IExcelHelper.GenerateReport()
    {
        Console.WriteLine("GenerateReport");
    }

    void ICoreExcelHelper.GenerateReport()
    {
        Console.WriteLine("CoreGenerateReport");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IExcelHelper obj = new GenerateExcelStrategySummary();
        obj.GenerateReport();

        obj.Test();

        ICoreExcelHelper obj1 = new GenerateExcelStrategySummary();
        obj1.GenerateReport();

        Console.WriteLine("Hello World!");
        Console.ReadLine();
    }
}

I would argue that you don't really gain anything by having the same definition in two different interfaces and that casting interfaces to concrete types is not a good approach. 我认为你并没有真正获得任何东西,因为在两个不同的接口中使用相同的定义,并且将接口转换为具体类型并不是一个好方法。 Both definitions in IExcelHelper and ICoreExcelHelper will do the same fundamental thing which is generate a report but in a different way. IExcelHelperICoreExcelHelper中的两个定义将执行相同的基本操作,即以不同的方式生成报告。 I would define one report interface and have the strategy object return an instance or take an instance as a dependency. 我将定义一个报表接口,让策略对象返回一个实例或将一个实例作为依赖项。 The power of using interfaces is being able to code to a behavior rather than a concrete type. 使用接口的强大功能是能够编码行为而不是具体类型。

public interface IGenerateReport {
  void GenerateReport();
  void Test();
}

public abstract class HelperBase : IGenerateReport {

  public abstract void GenerateReport();

  public void Test() {
    Console.WriteLine("Test");
  }

}

public class ExcelHelper : HelperBase {

  public override void GenerateReport() 
  {
    //I generate my report this way
  }

}

public class CoreHelper : HelperBase {

  public override void GenerateReport() 
  {
    //I will generate my report in a totally different way
  }

}

This will allow you to define your types as either HelperBase or IGenerateReport and you will have access to all your methods. 这将允许您将类型定义为HelperBaseIGenerateReport ,您将可以访问所有方法。

IGenerateReport helper = new ExcelHelper();
helper.Test();
helper.GenerateReport();

helper = new CoreHelper();
helper.Test();
helper.GenerateReport();

HelperBase helper2 = new ExcelHelper();
helper2.Test();
helper2.GenerateReport();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写显式接口I1 c = new customer(); - writing explicit interface I1 c=new customer(); 如何通过 Unity 中的 I2 本地化从 Google 表格中分配单元格(映射数据)? - How to assign cells (Mapping data) from Google Sheets by I2 Localization in Unity? 如何在一个类中同时具有抽象方法和虚拟方法? - How can I have both abstract and virtual methods in one class? 在这种情况下应该使用接口还是抽象类? - Should I use an interface or abstract class in this scenario? 我应该为抽象类创建接口吗? - Should I create an interface for abstract class? 我需要更新抽象类的所有方法吗? - Do I need to update all the methods of an abstract class? 如何确定类与属性(抽象和接口)的关系 - How can I determine the relationship of a class to properties (Abstract and Interface) 如何编写指定创建逻辑的接口或抽象类? - How do I write an interface or abstract class that specifies creation logic? 如何决定是使用接口还是抽象类? - How do I decide whether to use an interface or an abstract class? 我试图在一个类中实现抽象类和接口,只有接口成员被调用,而抽象方法没有? - I tried to implement abstract class and interface in a class and only interface members got called and abstract method didnt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM