继承设计问题 [英] Inheritance Design Question

查看:87
本文介绍了继承设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个抽象类来派生所有节点。在示例中,

基类中的Parent是Type Node,但在自定义类中我希望Parent返回Type CustomNode。当NodeNode由

辅助类访问时,Node Parent始终为null。这是一个糟糕的设计,我确定

这是一个常见的问题,我只是遗漏了一些东西。


注意:我使用虚拟为父,因为抽象不会编译。


公共抽象类节点

{

公共虚拟节点父级

{

get {return null; }

}

public virtual bool IsLastChild

{

get {return this._isLastChild; }

}

private bool _isLastChild = false;

}


公共类CustomNode:Node

{

public new CustomNode Parent

{

get {return this._customNode; }

}

private CustomNode _customNode = null;

}

I want to provide an abstract class to derive all nodes from. In the example
below Parent in the base class is of Type Node, but in the custom class I
want Parent to return a Type CustomNode. When CustomNode is accessed by a
helper class as Node Parent is always null. Is this a poor design, Im sure
it''s a common problem and I am just missing something.

Note: I use virtual for Parent because abstract will not compile.

public abstract class Node
{
public virtual Node Parent
{
get { return null; }
}
public virtual bool IsLastChild
{
get { return this._isLastChild; }
}
private bool _isLastChild = false;
}

public class CustomNode : Node
{
public new CustomNode Parent
{
get { return this._customNode; }
}
private CustomNode _customNode = null;
}

推荐答案

你需要在派生类中使用覆盖而不是新的。


Thomas P. Skinner [MVP]


"迈克" < MI ******** @ com.nospam>在留言中写道

新闻:O7 **************** @ TK2MSFTNGP11.phx.gbl ...
You need to use override rather than new in the derived class.

Thomas P. Skinner [MVP]

"Mike" <mi********@com.nospam> wrote in message
news:O7****************@TK2MSFTNGP11.phx.gbl...
我想要提供一个抽象类来派生所有节点。在下面的
示例中,基类中的Parent是Type Node,但在自定义类中我希望Parent返回Type CustomNode。当一个
辅助类访问CustomNode时,Node Parent始终为null。这是一个糟糕的设计,我确定
这是一个常见的问题,我只是缺少一些东西。

注意:我使用virtual for Parent,因为abstract不会编译。

公共抽象类Node
公共虚拟节点Parent
{
get {return null; }
}公共虚拟布尔IsLastChild
{
获取{return this._isLastChild; }
}
私有bool _isLastChild = false;
}
公共类CustomNode:Node
公共新的CustomNode Parent {
get {return this._customNode;私有CustomNode _customNode = null;
}
I want to provide an abstract class to derive all nodes from. In the
example
below Parent in the base class is of Type Node, but in the custom class I
want Parent to return a Type CustomNode. When CustomNode is accessed by a
helper class as Node Parent is always null. Is this a poor design, Im
sure
it''s a common problem and I am just missing something.

Note: I use virtual for Parent because abstract will not compile.

public abstract class Node
{
public virtual Node Parent
{
get { return null; }
}
public virtual bool IsLastChild
{
get { return this._isLastChild; }
}
private bool _isLastChild = false;
}

public class CustomNode : Node
{
public new CustomNode Parent
{
get { return this._customNode; }
}
private CustomNode _customNode = null;
}



更改时覆盖不起作用返回类型,我想返回

CustomNode派生自Node。
Override does not work when changing the return Type, I want to return
CustomNode which derives from Node.




" Mike" < MI ******** @ com.nospam>在消息中写道

news:e9 ************** @ TK2MSFTNGP14.phx.gbl ...

"Mike" <mi********@com.nospam> wrote in message
news:e9**************@TK2MSFTNGP14.phx.gbl...
覆盖不起作用更改返回类型,我想返回
CustomNode派生自Node。
Override does not work when changing the return Type, I want to return
CustomNode which derives from Node.




你真的应该返回Node。如果你知道它是CustomNode,你可以自由地把它投射到CustomNode。


David



You really should return Node. You are free to cast it to CustomNode if you
know it is a CustomNode.

David


这篇关于继承设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆