[英]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. IExcelHelper和ICoreExcelHelper中的两个定义都将执行相同的基本操作,即以不同的方式生成报告。 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. 这将允许您将类型定义为HelperBase或IGenerateReport ,您将可以访问所有方法。
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.