简体   繁体   English

继承返回泛型类型的函数

[英]Inheriting functions that return generic types

I'm attempting to write two classes, call 'em Parent and Child. 我正在尝试编写两个类,称为“父级和子级”。

parent is an abstract class that defines a function that returns a (sort of) generic type parent是一个抽象类,它定义一个返回一个(某种)泛型类型的函数

public abstract class Parent
{
     public abstract KeyValuePair<K,V> foo(someInputs);
}

with the intention being that Child can override this function to return a KeyValuePair with different types as the key and value. 目的是让Child重写此函数以返回具有不同类型的键和值的KeyValuePair。

public abstract class Child
{
     public override KeyValuePair<string,int> foo(someInputs)
     {
          return(new KeyValuePair<string,int>(someInput1,someInput2);
     }
}

This achieves the goal of communicating to the user developing the child(future me) that the function needs to return a KeyValuePair, but that I am free to use whatever KeyValuePair I want. 这样就达到了与开发该子级的用户(对我来说)的目的(该函数需要返回一个KeyValuePair,但我可以自由使用我想要的任何KeyValuePair)进行通信的目的。

Unfortunately though it seems the only way to achieve this is to also make the class generic, 不幸的是,尽管看来实现这一目标的唯一方法是使该类成为通用类,

public abstract class Parent<K,V>

and pass K and V down to the abstract function. 并将K和V传递给抽象函数。

This means that when I am writing the child, I need to define the same types for the class (which is now generic) and function return. 这意味着当我编写孩子时,我需要为类(现在是通用的)和函数返回定义相同的类型。 If they do not match, the compiler poops itself (with good reason). 如果它们不匹配,则编译器会自粪(有充分的理由)。

Is there a way to have these "semi generic" return types without making the class generic? 有没有办法让这些“半通用”返回类型不使该类通用?

Classes with different generic parameters are totally different things. 具有不同泛型参数的类是完全不同的东西。

KeyValuePair<string, int>

KeyValuePair<string, Graphics>

For example, the above two KeyValuePairs are not interchangeable. 例如,以上两个KeyValuePair是不可互换的。

Let say, you have 2 child classes: 假设您有2个子类:

public abstract class Child1 : Parent
{
    public override KeyValuePair<string, int> foo() { ... }
}

public abstract class Child2 : Parent
{
    public override KeyValuePair<string, Graphics> foo() { ... }
}

Parent class has no way to satisfy both children in one declaration. 父类无法在一个声明中同时满足两个孩子。

free to use whatever KeyValuePair I want 免费使用我想要的任何KeyValuePair

Therefore, such freedom is not logically correct. 因此,这种自由在逻辑上是不正确的。

You can have children implement a generic interface and a non-generic base class (if necessary)? 您可以让子类实现通用接口和非通用基类(如果需要)吗?

public abstract Parent
{
   // common non-generic methods
}

public interface IFooable<T>
{
    KeyValuePair<string, T> Foo();
}

public class Child1 : Parent, IFooable<int>
{
    public virtual KeyValuePair<string, int> Foo()
    {
    }
}

public class Child2 : Parent, IFooable<string>, IFooable<bool>
{
    public KeyValuePair<string, string> Foo()
    {
    }

    public KeyValuePair<string, bool> Foo()
    {
    }
}

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

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