简体   繁体   English

抽象类中的泛型方法

[英]generic method in abstract class

I have the following abstract base class in which i have an abstract method. 我有以下抽象基类,其中我有一个抽象方法。 I need to know how to implement this abstract method in the child classes. 我需要知道如何在子类中实现这个抽象方法。 The problem is how do I declare a class whose base is SomeBaseClass in Class B. 问题是如何在B类中声明一个基类为SomeBaseClass的类。

public abstract class A
{
    protected abstract void Add<T>(T number) where T : SomeBaseClass;
}

public class B : A
{
    protected override void Add<T>(T number)
    {
        throw new NotImplementedException();
    }
}

I think you want the base class to have a type parameter, not a specific method: 我想你希望基类有一个类型参数,而不是一个特定的方法:

public abstract class A<T> where T : SomeBaseClass
{
    protected abstract void Add(T number);
}

public class B : A<C> {

    protected void Add(C number) { ... }
}

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

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