c#类继承自泛型,方法返回类型
我有一个泛型类,它应该是一个树,我希望继承这样的类:c#类继承自泛型,方法返回类型,c#,generics,casting,inheritance,C#,Generics,Casting,Inheritance,我有一个泛型类,它应该是一个树,我希望继承这样的类: public class Tree<T> { private HashSet<Tree<T>> leaves; private T data; public Tree() { leaves = new HashSet<Tree<T>>(); } public Tree(T data) : this() { t
public class Tree<T> {
private HashSet<Tree<T>> leaves;
private T data;
public Tree() {
leaves = new HashSet<Tree<T>>();
}
public Tree(T data) : this() {
this.data = data;
}
public T Data {
get {
return this.data;
}
set {
data = value;
}
}
public virtual Tree<T> findInLeaves(T data) {
foreach(Tree<T> leaf in leaves) {
if(leaf.Data.Equals(data)) {
return leaf;
}
}
return null;
}
}
public class ComboTree : Tree<IComboAction> {
private ComboMovement movement;
public ComboTree() : base() {
Movement = null;
}
public ComboTree(IComboAction action) : base(action) {
Movement = null;
}
public ComboMovement Movement {
get {
return this.movement;
}
set {
movement = value;
}
}
}
问题是为什么以及如何修复它
编辑:我创建了控制台程序,运行它,它就可以工作了。这一定是我的引擎出了问题 公共组合树(IComboAction操作)
public ComboTree(IComboAction action)
: base(action)
{
Movement = null; // <---- You are nulling Movement in the second constructor
}
:基本(动作)
{
Movement=null;//返回的对象与传递给父泛型类构造函数的对象相同。向我们展示root
是如何初始化的。不,您没有一个简单的泛型类“像这样”,因为“this”不会编译。您有真实的示例吗?抱歉,但细节很重要。特别是,“'GenericTree'不包含接受0个参数的构造函数"-我们真的需要查看构造函数的用法来显示创建根和调用的代码。我真的很想显示整个代码,但这些都是引擎脚本,有许多对其API的引用,这就是为什么我试图简化代码。为什么你没有添加树和子树的方法呢?事实上,你只有一个主干和一些数据和一组具有相同类型数据的叶子。不是真正的树。也许你应该给它取个别的名字?
public ComboTree(IComboAction action)
: base(action)
{
Movement = null; // <---- You are nulling Movement in the second constructor
}