简单的继承不起作用?

Mat*_*att 1 c#

我似乎忘记了一些最基本的继承规则,因为我无法弄清楚为什么这不起作用.我有一个扩展Node的类SuffixNode.

节点:

class Node
{
    public char label;
    public Node parent;
    public Dictionary<char,Node> children;

    public Node(Node NewParent, char NewLabel)
    {
        this.parent = NewParent;
        this.label = NewLabel;
        children=new Dictionary<char,Node>();
    }
}
Run Code Online (Sandbox Code Playgroud)

SuffixNode:

class SuffixNode: Node
{
    public Dictionary<String, int> Location=new Dictionary<String, int>();

    public SuffixNode(Node NewParent):base(NewParent, '$')
    {

    }

    public void AddLocation(String loc,int offset)
    {
        this.Location.Add(loc, offset);
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图从SuffixNode类调用主程序中的AddLocation方法,但是它给出了一个错误,说明没有这样的方法(在Node类中):

Node n;
char FirstChar = suffix[0]; //first character of the suffix 
if (suffix == "")
{
     return true;
}

//If the first character of a suffix IS NOT a child of the parent
if (!parent.children.ContainsKey(FirstChar))
{
     if (FirstChar == '$')
     {
          n = new SuffixNode(parent);
          n.AddLocation(document, offset);
     }
     else
     {
          n = new Node(parent, FirstChar); //Create a new node with the first char of the suffix as the label
          parent.children.Add(FirstChar, n); //Add new node to the children collection of the parent
     }
}          
Run Code Online (Sandbox Code Playgroud)

我确信这是一个非常简单的答案,但我无法理解为什么这不起作用.不能

Node n = new SuffixNode(parent)
Run Code Online (Sandbox Code Playgroud)

允许我访问SuffixNode方法和变量?

Ale*_*win 5

您已将n的类型声明为Node,实际上它没有AddLocation方法.你必须声明它SuffixNode n = new SuffixNode(parent)能够在其上调用子函数,否则添加一个(也许是抽象的)方法来Node调用AddLocation.