Inherited Generics Constructor C #

Hi, I am trying to create a generic treenode. Here is an abstract general 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 inherited from both of them:

 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; } } 

Error: "Parser.Objects.TreeNode" does not contain a constructor that takes 0 arguments

Help Pls!

+4
source share
4 answers

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

And, subsequently, you do not need to set properties inside the FHXTreeNode constructor, since it is processed in the constructor of the base class. Your new constructor should look like this:

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

You need to call the base class constructor. If you omit the call :base(...) , the baseless constructor of the base class is called. Since your base class does not have such a constructor, you get an error message.

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

Calling the parameterized constructor of the base class also obsolete the field initializations, since they are already assigned in the base class.

In C #, you cannot inherit constructors. You create a new constructor in a derived class that has the same signature as the base class constructor.


Your interface is also broken: you cannot declare methods as public abstract in the interface. Interface methods are always implicitly public and never have an implementation. Thus, these modifiers will be redundant and illegal.


Further, you cannot override using interface methods. You can override virtual / abstract methods from the base class. When you have a method that matches a method in an interface, that interface method is implemented.


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


There are also several stylistic issues: interface names must have the prefix I And I would avoid protected fields where possible.

+5
source

You must call the base class constructor from the FHXTreeNode constructor:

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

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

+3
source

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 constructor without parameters:> /

 public TreeNode() { } 

... then an FHXTreeNode instance cannot be created without calling its base constructor as follows:

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

Source: https://habr.com/ru/post/1395430/


All Articles