简体   繁体   English

继承模板抽象类C#

[英]Inheriting a template abstract class C#

To save myself some writing, you to save you some reading, I constructed a basic example to help me explain what I am trying to achieve. 为了节省写作时间,节省阅读时间,我构建了一个基本示例来帮助我解释我要达到的目标。

Say you have a program that uses both a stack class and a queue class, and both classes have the same methods/functions, an optimal choice would probably be to create ab abstract class and inherit it in both classes. 假设您有一个同时使用堆栈类和队列类的程序,并且两个类都具有相同的方法/函数,那么最佳选择可能是创建一个ab抽象类并在两个类中继承它。 If the classes are generic, then the abstract class has to be generic as well. 如果类是通用的,则抽象类也必须是通用的。 So here's an example of how the abstract class would look like: 因此,这里有一个抽象类的示例:

public abstract class Sequence <T>
{
    public abstract void push(T value);
    public abstract T pop();
    public abstract T peek();
    public abstract int size();
    public abstract bool isEmpty();
}

And let's assume that the following is the Stack class that'll need to inherit the Sequence class: 并假定以下是继承继承Sequence类的Stack类:

public class Stack<T>
{
    private List<T> stk;

    public Stack ()
    {
        stk = new List<T>();
    }

    public void push(T value)
    {
        stk.Add(value);
    }

    public T pop()
    {
        T topValue = peek();
        stk.RemoveAt(size () - 1);
        return topValue;
    }

    public T peek()
    {
        return stk[size() - 1];
    }

    public int size()
    {
        return stk.Count;
    }

    public bool isEmpty()
    {
        return size() == 0;
    }
}

Now, my question is, how would you inherit the Sequence class from Stack ? 现在,我的问题是,如何从Stack继承Sequence类? As you may have noticed already, what I am trying to get right is the syntax, not the functionality of the program. 正如您可能已经注意到的那样,我试图弄清的是语法,而不是程序的功能。 I wasn't able to find a solution that directly applies to my problem. 我找不到直接适用于我的问题的解决方案。

how would you inherit the Sequence class from Stack? 您将如何从Stack继承Sequence类?

Not. 不。 First thing, you need to turn this around, Stack has to derive from Sequence. 首先,您需要解决这个问题,Stack必须从Sequence派生。

Second point, when Sequence<T> only contains abstract members you might as well make it an interface . 第二点,当Sequence<T>仅包含抽象成员时,您也可以使其成为一个interface More flexible in the long run. 从长远来看更加灵活。

And the actual syntax is not so complicated: 实际的语法并不那么复杂:

public interface ISequence <T>
{
    void Push(T value);
    T Pop();
    ...
}

public class Stack<T> : ISequence<T>
{    
    public void Push(T value)
    {
        stk.Add(value);
    }
    ...
}

(I changed the capitalization to standard C# conventions) (我将大写字母更改为标准C#约定)

And the same syntax applies to abstract class Sequence : 同样的语法也适用于abstract class Sequence

public class Stack<T> : Sequence<T> 
{ 
    public override void Push(T value)
    {
        stk.Add(value);
    }
    .... 
}

I made 2 changes to your code: 我对您的代码进行了2次更改:

added override for the methods 为方法添加了override

The actual inheritance 实际继承

public class Stack<T> : Sequence<T>
{
    private List<T> stk;

    public Stack()
    {
        stk = new List<T>();
    }

    override public void push(T value)
    {
        stk.Add(value);
    }

    override public T pop()
    {
        T topValue = peek();
        stk.RemoveAt(size() - 1);
        return topValue;
    }

    override public T peek()
    {
        return stk[size() - 1];
    }

    override public int size()
    {
        return stk.Count;
    }

    override public bool isEmpty()
    {
        return size() == 0;
    }
}

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

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