C# 继承和覆盖基本属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# inheritance and overriding base properties
提问by We Are All Monica
I currently have a need for a custom ListViewItem
class - let's call it MyListViewItem
. It needs to have some additional data associated with each item, and perform some operations when the Checked property is changed. I've tried several things, but currently the relevant code looks like this:
我目前需要一个自定义ListViewItem
类 - 我们称之为MyListViewItem
. 它需要有一些与每个项目相关联的附加数据,并在 Checked 属性更改时执行一些操作。我已经尝试了几件事,但目前相关代码如下所示:
class MyListViewItem : ListViewItem {
new public bool Checked {
get {
return base.Checked;
}
set {
base.Checked = value;
// do some other things here based on value
}
}
public MyListViewItem(Object otherData) {
// ...
}
}
The problem I'm having is that when I click on the item's checkbox in the ListView, my setter is never called. Does anyone know what I am doing wrong? I'm aware that I could use the ItemChecked event of the parent ListView, but that seems like a much less clean solution. (Also I'm not actually passing an Object to the constructor, but that part isn't important here).
我遇到的问题是,当我单击 ListView 中项目的复选框时,从未调用过我的 setter。有谁知道我做错了什么?我知道我可以使用父 ListView 的 ItemChecked 事件,但这似乎是一个不太干净的解决方案。(此外,我实际上并没有将 Object 传递给构造函数,但这部分在这里并不重要)。
采纳答案by Quibblesome
It's not working cause the "new" keyword doesn't override it just "hides".
它不起作用,因为“new”关键字不会覆盖它只是“隐藏”。
This means that if you call Checked on an instance of object that is referenced through the type definition of MyListViewItem you willrun your code. However the ListView references to this object via the type definition of ListViewItem and therefore will not call your "new" method.
这意味着,如果您对通过 MyListViewItem 的类型定义引用的对象实例调用 Checked,您将运行您的代码。但是,ListView 通过 ListViewItem 的类型定义引用此对象,因此不会调用您的“新”方法。
"new" is notoverride. The better solution is to probably handle the code in a custom list view. It isn't really that ugly.
“新”不是覆盖。更好的解决方案可能是在自定义列表视图中处理代码。真的没有那么丑。
回答by Mehrdad Afshari
new
does not override
the base
member. It declares a new method with the same name. In VB.NET it's called Shadows
.
new
没有override
的base
成员。它声明了一个同名的新方法。在 VB.NET 中,它被称为Shadows
.
Indeed, new
doesn't do anything except turning off a compiler warning. The member you do not declare as override
(and you can only do this if the base
member is virtual
or override
) will be completely unrelated to the inheritance tree of the base
member.
实际上,new
除了关闭编译器警告外,什么都不做。您未声明为的成员override
(并且您只能在该base
成员为virtual
或时才可以这样做override
)将与该base
成员的继承树完全无关。
回答by John Saunders
Assuming that the ListViewItem
.Checked
property is virtual, you need to override it:
假设ListViewItem
. Checked
属性是虚拟的,您需要覆盖它:
public override bool Checked
回答by Brian ONeil
The ListViewItem.Checked property is not virtual (see MSDN doc here) so you will not be able to override the behavior of it in this way. You will have to use the event or derive from ListView and override ListView.OnItemCheckedto change the behavior.
ListViewItem.Checked 属性不是虚拟的(请参阅此处的MSDN 文档),因此您将无法以这种方式覆盖它的行为。您必须使用该事件或从 ListView 派生并覆盖ListView.OnItemChecked以更改行为。
回答by Robert Venables
Instead of creating your own custom ListViewItem, why not create a seperate type to contain your custom data and then assign each ListViewItem Tag property with a reference to the custom data?
与其创建您自己的自定义 ListViewItem,不如创建一个单独的类型来包含您的自定义数据,然后使用对自定义数据的引用来分配每个 ListViewItem 标签属性?
This is the pattern that I've been using for some time and it works very well. As for custom action when items are checked, simply handle the relevant events in the list view.
这是我已经使用了一段时间的模式,并且效果很好。至于勾选项目时的自定义动作,只需在列表视图中处理相关事件即可。