派生类的继承方式是否不同?
System.Web.UI.WebControls.UI.TableHeaderCell
派生自
System.Web.UI.WebControls.UI.TableCell
So 具有签名 foo(TableCell tc){}
的方法
将接受 TableHeaderCell 的实例。
但是,如果我创建两个新类,一个从 TableCell
派生,另一个从 TableHeaderCell
派生,那么我的新 TableHeaderCell 类显然无法转换为我的新 TableCell 类,因为它们没有直接关系。
有办法解决这个问题吗?我猜不会。
System.Web.UI.WebControls.UI.TableHeaderCell
derives from
System.Web.UI.WebControls.UI.TableCell
So a method with the signature foo(TableCell tc){}
Will accept instances of TableHeaderCell.
However if i create two new classes and derive one from TableCell
and the other from TableHeaderCell
then my new TableHeaderCell class will obviously not be able to cast into my new TableCell class as they are not directly related.
Is there anyway to get around this? Im guessing no.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是说这样的等级制度吗?
不可以,在这种情况下,您无法将
CustomTableHeaderCell
转换为CustomTableCell
。你确定在这种情况下你真的需要继承吗?你能用组合来代替吗?或者您可以让
CustomTableCell
和CustomTableHeaderCell
实现ICustomCell
接口吗?如果您可以向我们提供有关您正在尝试做什么的更多信息,那将会有所帮助。You mean a hierarchy like this?
No, in that case you couldn't cast
CustomTableHeaderCell
toCustomTableCell
.Are you sure you really need inheritance in this case? Could you use composition instead? Or could you make
CustomTableCell
andCustomTableHeaderCell
implement anICustomCell
interface? If you could give us more information about what you're trying to do, that would help.您可以继续将它们全部引用为 TableCell 对象,至少如果目标是能够调用您的 foo() 的话。
You could just keep referring to all of them as TableCell objects, at least if the goal is to be able to call your foo().