C# 继承的泛型构造函数
嗨,我正在尝试制作一个通用的树节点。这是抽象泛型类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要添加对基类构造函数的调用。
并且,随后,您不需要在 FHXTreeNode 的构造函数内部设置属性,因为它是在基类的构造函数中处理的。您的新构造函数应如下所示:
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:
您需要调用基类的构造函数。如果省略
:base(...)
调用,则会调用基类的无参数构造函数。由于您的基类没有这样的构造函数,因此您会收到错误。调用参数化基类构造函数也会使字段初始化变得过时,因为它们已经在基类中分配了。
在 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.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 overridevirtual
/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 writeAddChild(T child);
.There are also a few stylistic issues: Interface names should be prefixed with
I
. And I'd avoidprotected
fields where possible.您必须从 FHXTreeNode 构造函数调用基类构造函数:
另外,按照惯例,接口在 .NET 中以大写 I 开头,因此 TreeNodeOperations 应该是 ITreeNodeOperations
You have to call the base class constructor from your FHXTreeNode constructor:
Also, by convention, interfaces start with an uppercase I in .NET, so TreeNodeOperations should be ITreeNodeOperations
首先,
public
和abstract
不是接口声明中的有效关键字。您的界面应如下所示:为了回答您的问题,由于
TreeNode
没有像这样定义无参数构造函数:...那么如果不调用其基本构造函数,就无法创建 FHXTreeNode 的实例,如下所示:
First of all,
public
andabstract
are not valid keywords in interface declarations. Your interface should look like this:To answer your question, since
TreeNode
does not define a parameterless constructor like so:...then an instance of FHXTreeNode cannot be created without calling its base constructor like so: