簡體   English   中英

如何定義繼承通用抽象類的通用抽象類?

[英]How to define a generic abstract class that inherits a generic abstract class?

我有一個繼承一個抽象類的類,該抽象類又繼承了另一個抽象類。 我可以將最高級的類定義為泛型,但是在最終ListResults類中表達所需的實際類型之前,我不知道如何將中級類定義為泛型。

internal abstract class LowerCommand<T> {
    public abstract void Execute();
    public abstract List<T> ExecuteList();
}

// currently gives error for `T`
internal abstract class HigherCommand : Lowercommand<T> {
    // ... defines other stuff, nothing to do with
    //     already instantiated methods or T ...
}

class ListResults : HigherCommand<Results>
{
    public override void Execute() {...}
    public override List<Results> ExecuteList() {...}
}

謝謝你的幫助。

您仍然需要在HigherCommand的定義上定義通用類型參數T ,以便可以依次正確定義LowerComand

internal abstract class LowerCommand<T> {
    public abstract void Execute();
    public abstract List<T> ExecuteList();
}

// Note that HigherCommand require a declaration of `T`
internal abstract class HigherCommand<T> : Lowercommand<T> {
    // ... defines other stuff, nothing to do with
    //     already instantiated methods or T ...
}

class ListResults : HigherCommand<Results>
{
    public override void Execute() {...}
    public override List<Results> ExecuteList() {...}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM