C# 继承的泛型构造函数

发布于 2025-01-04 05:16:58 字数 1346 浏览 1 评论 0原文

嗨,我正在尝试制作一个通用的树节点。这是抽象泛型类

public abstract class TreeNode<T>
{
    protected List<TreeNode<T>> _childNodes = new List<TreeNode<T>>();
    protected TreeNode<T> ParentNode;

    public T ObjectData { get; set; }

    public TreeNode( TreeNode<T> parent, T data)
    {
        ParentNode = parent;
        ObjectData = data;
    }    
}

它有一个伴随接口

interface TreeNodeOperations<T>
{
    //Adds child to tree node
    public abstract void AddChild<T>(T child);
    //Performs N-Tree search
    public abstract TreeNode<T> SeachChild<T>(T child);
}

我想做的是从这两个接口继承:

public class FHXTreeNode<T>: TreeNode<T>, TreeNodeOperations<T> where T : ParserObject
{
    public FHXTreeNode(FHXTreeNode<T> parent, T data) ---> # **ERROR** #
    {
        ParentNode = parent;
        ObjectData = data;
    }

    //Adds child to tree node
    public override FHXTreeNode<T> AddChild<ParserObject>(T childData)
    {
        FHXTreeNode<T> child = new FHXTreeNode<T>(this, childData);

        //_childNodes.Add(child);        

        return child;
    }

}

错误是:'Parser.Objects.TreeNode'不包含采用 0 的构造函数争论

请帮忙!

Hi I'm trying to make a generic treenode. Here is the abstract generic class

public abstract class TreeNode<T>
{
    protected List<TreeNode<T>> _childNodes = new List<TreeNode<T>>();
    protected TreeNode<T> ParentNode;

    public T ObjectData { get; set; }

    public TreeNode( TreeNode<T> parent, T data)
    {
        ParentNode = parent;
        ObjectData = data;
    }    
}

It has a companion interface

interface TreeNodeOperations<T>
{
    //Adds child to tree node
    public abstract void AddChild<T>(T child);
    //Performs N-Tree search
    public abstract TreeNode<T> SeachChild<T>(T child);
}

What I'm trying to do is inherit from both of these:

public class FHXTreeNode<T>: TreeNode<T>, TreeNodeOperations<T> where T : ParserObject
{
    public FHXTreeNode(FHXTreeNode<T> parent, T data) ---> # **ERROR** #
    {
        ParentNode = parent;
        ObjectData = data;
    }

    //Adds child to tree node
    public override FHXTreeNode<T> AddChild<ParserObject>(T childData)
    {
        FHXTreeNode<T> child = new FHXTreeNode<T>(this, childData);

        //_childNodes.Add(child);        

        return child;
    }

}

The error is: 'Parser.Objects.TreeNode' does not contain a constructor that takes 0 arguments

Help Pls!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

○愚か者の日 2025-01-11 05:16:58

您需要添加对基类构造函数的调用。

并且,随后,您不需要在 FHXTreeNode 的构造函数内部设置属性,因为它是在基类的构造函数中处理的。您的新构造函数应如下所示:

public FHXTreeNode(FHXTreeNode<T> parent, T data) : base(parent, data)
{
}

You need to add a call to the base class' constructor.

And, subsequently, you don't need to set the properties inside of FHXTreeNode's constructor since it's handled in the base class' constructor. Your new constructor should look like this:

public FHXTreeNode(FHXTreeNode<T> parent, T data) : base(parent, data)
{
}
如果没结果 2025-01-11 05:16:58

您需要调用基类的构造函数。如果省略 :base(...) 调用,则会调用基类的无参数构造函数。由于您的基类没有这样的构造函数,因此您会收到错误。

public FHXTreeNode(FHXTreeNode<T> parent, T data)
   :base(parent, data)
{
}

调用参数化基类构造函数也会使字段初始化变得过时,因为它们已经在基类中分配了。

在 C# 中,您无法继承构造函数。您在派生类中创建一个新的构造函数,该构造函数恰好与基类构造函数具有相同的签名。


你的接口也被破坏了:你不能在接口中将方法声明为公共抽象。接口方法始终是隐式公共的,并且永远不会有实现。所以这些修饰符是多余的,而且是非法的。


接下来,您不能覆盖接口方法。您只能从基类重写虚拟/抽象方法。当您有一个与接口中的方法匹配的方法时,就会实现该接口方法。


另一个错误是您在接口方法上重用类型参数:void AddChild(T child); 是错误的。此语法用于在方法上引入类型参数。但您想使用包含类型中的类型参数。所以你应该写AddChild(T child);


还有一些风格问题: 接口名称应以 I 为前缀。我会尽可能避免使用 protected 字段。

You need to call the constructor of the base class. If you omit the :base(...) call, the parameterless constructor of the base class gets called. Since your base class doesn't have such a constructor, you get an error.

public FHXTreeNode(FHXTreeNode<T> parent, T data)
   :base(parent, data)
{
}

Calling the parameterized base class constructor makes the field initializations obsolete too, since they already get assigned to in the base class.

In C# you can't inherit constructors. You create a new constructor in the derived class that happens to have the same signature as the base class constructor.


Your interface is broken too: You can't declare methods as public abstract in an interface. Interface methods are always implicitly public, and never have an implementation. So those modifiers would be redundant, and are illegal.


Next you can't override interface methods. You can only override virtual/abstract methods from a base class. When you have a method which matches a method in an interface, in implements that interface method.


Yet another mistake is that you reuse the type parameter on the interface methods: void AddChild<T>(T child); is wrong. This syntax is for introducing type parameters on a method. But you want to use the type parameter from the containing type. So you should write AddChild(T child);.


There are also a few stylistic issues: Interface names should be prefixed with I. And I'd avoid protected fields where possible.

泛滥成性 2025-01-11 05:16:58

您必须从 FHXTreeNode 构造函数调用基类构造函数:

public FHXTreeNode(FHXTreeNode<T> parent, T data)
    : base(parent, data)
{
    ParentNode = parent;
    ObjectData = data;
}

另外,按照惯例,接口在 .NET 中以大写 I 开头,因此 TreeNodeOperations 应该是 ITreeNodeOperations

You have to call the base class constructor from your FHXTreeNode constructor:

public FHXTreeNode(FHXTreeNode<T> parent, T data)
    : base(parent, data)
{
    ParentNode = parent;
    ObjectData = data;
}

Also, by convention, interfaces start with an uppercase I in .NET, so TreeNodeOperations should be ITreeNodeOperations

荒芜了季节 2025-01-11 05:16:58

首先,publicabstract 不是接口声明中的有效关键字。您的界面应如下所示:

interface TreeNodeOperations<T>
{
    //Adds child to tree node
    void AddChild(T child);
    //Performs N-Tree search
    TreeNode<T> SeachChild(T child);
}

为了回答您的问题,由于 TreeNode 没有像这样定义无参数构造函数:

public TreeNode() { }

...那么如果不调用其基本构造函数,就无法创建 FHXTreeNode 的实例,如下所示:

public FHXTreeNode(FHXTreeNode<T> parent, T data) : base(parent, data) { }

First of all, public and abstract are not valid keywords in interface declarations. Your interface should look like this:

interface TreeNodeOperations<T>
{
    //Adds child to tree node
    void AddChild(T child);
    //Performs N-Tree search
    TreeNode<T> SeachChild(T child);
}

To answer your question, since TreeNode does not define a parameterless constructor like so:

public TreeNode() { }

...then an instance of FHXTreeNode cannot be created without calling its base constructor like so:

public FHXTreeNode(FHXTreeNode<T> parent, T data) : base(parent, data) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文