简体   繁体   English

子类继承相同接口的不同实现

[英]Child classes inheriting different implementations of the same interface

Okay, I have a small hiccup. 好吧,我有点打small。

I want to implement an interface with a generic parameter type, and then extend that implementation with another parameter type, and not have to do the initialization all over again. 我想用通用参数类型实现一个接口,然后用另一个参数类型扩展该实现,而不必再次进行初始化。

Basically, I have a similar structure to this: 基本上,我有与此类似的结构:

interface IBase<MemberType>;

abstract class Base : IBase<OneMemberType> {

  protected OneMemberType member;
  public void init() {
    member = new OneMemberType();
  }
}


class Extended: Base, IBase<AnotherMemberType> {

}

I want class Extended to have a "member" property of the "AnotherMemberType" type. 我希望扩展类具有“ AnotherMemberType”类型的“成员”属性。

AnotherMemberType and OneMemberType both implement the same interface. AnotherMemberType和OneMemberType都实现相同的接口。

Any ideas how i could do that, without explicitly defining it in the Extended class? 没有在Extended类中显式定义它的任何想法,我该怎么做?

This is the actual interface. 这是实际的界面。

interface ILayer<TextureProviderType>
{
    Texture2D getTexture();         
    byte[,] getArray();              

    void generate();
}

Updated 更新

I'm trying to have specific objects that extend the BaseLayer class and that each use a specific TextureProvider child, and only have to instantiate the texture providers in the base class. 我正在尝试具有扩展BaseLayer类的特定对象,并且每个对象都使用特定的TextureProvider子级,并且仅需实例化基类中的纹理提供程序。

Update 2 Okay, it has nothing to do with interfaces apparently. 更新2好吧,这显然与接口无关。 What I want to have is a generic base class member, that can be assigned a type in the extended children classes. 我想要的是一个通用基类成员,可以在扩展子类中为其分配类型。

You can implement the interface methods just once and it will work for both interfaces. 您只需实施一次接口方法,它将对两个接口都有效。

If you want different implementations per interface, you must use explicit implementations or use the generic type parameter in the signature of your interface methods. 如果要每个接口使用不同的实现,则必须在接口方法的签名中使用显式实现或使用通用类型参数。

Point is, you can't have 2 methods with the same signature in your class and have the CLR know which one to use. 重点是,您不能在类中拥有2个具有相同签名的方法,并且CLR知道要使用哪个方法。


Edit: after reading you comment on the problem you're trying to solve: 编辑:在阅读您对要解决的问题的评论后:

Wouldn't a generic type parameter in your BaseLayer + inheritance in your TextureProviders work? 您的BaseLayer +继承中的通用类型参数在TextureProviders中不起作用吗? Would look something like this: 看起来像这样:

class TextureProvider
{
    public TextureProvider()
    {

    }

    public void Foo()
    {

    }
}

class SpecialTextureProvider : TextureProvider
{
    public SpecialTextureProvider()
        : base()
    {

    }
}

class BaseLayer<TP> where TP : TextureProvider, new()
{
    public BaseLayer()
    {
        var tp = new TP();
    }
}

class SpecificLayer : BaseLayer<SpecialTextureProvider>
{
}

Alternatively, you could use the factory pattern. 或者,您可以使用工厂模式。

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

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