简体   繁体   English

接口和抽象C#继承

[英]Interface and abstract c# inheritance

I am encountering a problem with interfaces. 我遇到接口问题。 I wish to chain a method which derives its chainable method from an abstract class, which implements an interface. 我希望链接一个方法,该方法从实现接口的抽象类派生其可链接方法。

public interface IBaseInterface {
    public IBaseInterface ChainableMethod()
}

public abstract AbstractClassThatHelps<T> where T: IBaseInterface n {
    public T ChainableMethod() {
        return (T) this;
    }
}

public interface IDerived : IBaseInterface { }

public class DerivedClass : AbstractClassThatHelps<IDerived>, IDerived { }

IDerived derived =  new DerivedClass();
derived.ChainableMethod().ChainableMethod();

The problem I face here: why can't T be returned when it is shown to implement the contract IModel? 我在这里面临的问题:为什么在显示实现合同IModel时不能返回T

How would I solve this differently? 我该如何解决呢? I wish to have type safety but I am forced for all derived classes to return IBaseInterface instead of their own interface. 我希望具有类型安全性,但是我被迫让所有派生类返回IBaseInterface而不是它们自己的接口。


Actual implementation: We have multiple models ( DerivedClass 's) which implement their respective IDerived for dependency injection. 实际实现:我们有多个模型( DerivedClass ),它们实现了各自的IDerived用于依赖项注入。 These need helpers because i dont want to repeat myself.. So we use AbstractClassThatHelps as a base, but because we are dealing with chainable methods we need this base class to know what to return, so therefore generics. 这些需要帮助者,因为我不想重复自己。.因此,我们使用AbstractClassThatHelps作为基础,但是由于我们正在处理可链接的方法,因此我们需要该基础类来知道要返回的内容,因此要知道泛型。 IBaseInterface can be seen as IModel . IBaseInterface可以看作IModel Where ChainableMethod can be seen as GetAll() for example. 例如,其中ChainableMethod可以视为GetAll()

In order for the following code to work AbstractClassThatHelps<T> must implement IBaseInterface . 为了使以下代码起作用, AbstractClassThatHelps<T>必须实现IBaseInterface How can you return this , if this is not IBaseInterface 你怎么能回this ,如果this不是IBaseInterface

public abstract AbstractClassThatHelps<T> where T: IBaseInterface n{
    public T ChainableMethod(){
        return this;
    }
}

Edit: I am not user what this design solves but here is my attempt at what you are trying to achieve→ 编辑:我不是用户此设计解决的问题,但这是我对您要实现的目标的尝试→

public interface IBaseInterface
{
    IBaseInterface ChainableMethod();
}

public abstract class AbstractClassThatHelps<T>:IBaseInterface where T : IBaseInterface{
    public T ChainableMethod()
    {
        IBaseInterface i = this;
        return (T)i.ChainableMethod();
    }

    IBaseInterface IBaseInterface.ChainableMethod()
    {
       return this;
    }
 }

public class Concrete : AbstractClassThatHelps<Concrete>
{
}

您可以返回T实例 ,但是返回类型不能为T而必须为IBaseInterface因为这是接口所需要的。

This is working, your code was full of syntax errors: 这可以正常工作,您的代码充满语法错误:

public interface IBaseInterface
{
    IBaseInterface ChainableMethod();
}

public abstract class AbstractClassThatHelps : IBaseInterface
{
    public IBaseInterface ChainableMethod()
    {
        return this;
    }
}

public interface IDerived : IBaseInterface
{

}

public class DerivedClass : AbstractClassThatHelps, IDerived
{

}


internal static class Program
{
    public static void Main()
    {

        IDerived derived = new DerivedClass();
        derived.ChainableMethod().ChainableMethod();
    }
}

you could also try this: 您也可以尝试以下方法:

public interface IBaseInterface
{
    IBaseInterface ChainableMethod();
}

public abstract class AbstractClassThatHelps<T> : IBaseInterface where T : class, IBaseInterface
{
    public T ChainableMethod()
    {
        return this as T;
    }

    IBaseInterface IBaseInterface.ChainableMethod()
    {
        return ChainableMethod();
    }

}

public interface IDerived : IBaseInterface
{
    IDerived Hello();
}

public class DerivedClass : AbstractClassThatHelps<IDerived>, IDerived
{
    public IDerived Hello()
    {
        Console.WriteLine("Hello");
        return this;
    }
}


internal static class Program
{
    public static void Main()
    {

        AbstractClassThatHelps<IDerived> derived = new DerivedClass();
        derived.ChainableMethod().Hello().ChainableMethod();
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM