C# 方法重写/隐藏C语言中的行为#
我对C#的理解是,子类不能重写父类的方法实现,除非该方法被标记为虚拟。如果子类声明了一个与父类方法同名的方法,而父类方法未标记为虚拟,则它只是隐藏该方法,这样,如果从父类类型的引用调用该方法,它将调用父方法,如果从子类类型的引用调用该方法,它将调用子类方法。然而,我在C#library中发现了一种情况,似乎破坏了这种行为C# 方法重写/隐藏C语言中的行为#,c#,inheritance,polymorphism,overriding,C#,Inheritance,Polymorphism,Overriding,我对C#的理解是,子类不能重写父类的方法实现,除非该方法被标记为虚拟。如果子类声明了一个与父类方法同名的方法,而父类方法未标记为虚拟,则它只是隐藏该方法,这样,如果从父类类型的引用调用该方法,它将调用父方法,如果从子类类型的引用调用该方法,它将调用子类方法。然而,我在C#library中发现了一种情况,似乎破坏了这种行为 集合声明了一个方法public void Add(T项)。 此方法不是虚拟的,因此子类中的实现不应重写其行为。然而,下面的测试产生了一个矛盾的结果 公共无效测试() { Obs
集合
声明了一个方法public void Add(T项)
。
此方法不是虚拟的,因此子类中的实现不应重写其行为。然而,下面的测试产生了一个矛盾的结果
公共无效测试()
{
ObservableCollection strings1=新的ObservableCollection();
strings1.CollectionChanged+=OnCollectionChanged;
字符串1.添加(“一个字符串”);
收集字符串2=字符串1;
strings2.添加(“另一个字符串”);
}
CollectionChanged(对象源、,
NotifyCollectionChangedEventArgs(e)
{
Console.WriteLine(“集合更改!”);
}
由于NotifyCollectionChanged
行为未在Collection
类中实现,并且observedcollection
类无法重写Collection
类的Add
方法,我希望仅当对象被引用为可观察集合
时才会触发集合更改事件,而不是当对象被引用为集合
时。但有两起事件被触发。结果是:
Collection Change!
Collection Change!
有人能解释一下这里发生了什么吗?ObservableCollection没有自己的Add方法。相反,它依赖于集合类Add,即:
public class Collection<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
public void Add(T item)
{
if (this.items.IsReadOnly)
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
this.InsertItem(this.items.Count, item);
}
protected virtual void InsertItem(int index, T item)
{
this.items.Insert(index, item);
}
}
公共类集合:IList、ICollection、IList、ICollection、IReadOnlyList、IReadOnlyCollection、IEnumerable、IEnumerable
{
公共作废新增(T项)
{
如果(此.items.IsReadOnly)
ThrowHelper.ThrownNotSupportedException(ExceptionResource.NotSupported\u ReadOnlyCollection);
this.InsertItem(this.items.Count,item);
}
受保护的虚拟void插入项(int索引,T项)
{
本.项目.插入(索引,项目);
}
}
InsertItem是一个虚拟方法,在ObservaleCollection中被重写
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
protected override void InsertItem(int index, T item)
{
this.CheckReentrancy();
base.InsertItem(index, item);
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Add, (object) item, index);
}
}
公共类ObservableCollection:Collection、INotifyCollectionChanged、INotifyPropertyChanged
{
受保护的覆盖无效插入项(int索引,T项)
{
this.CheckReentrancy();
基本插入项(索引,项目);
本.不动产变更(“计数”);
本.OnPropertyChanged(“第[]项”);
this.OnCollectionChanged(NotifyCollectionChangedAction.Add,(对象)项,索引);
}
}
ObservableCollection没有自己的Add方法。相反,它依赖于集合类Add,即:
public class Collection<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
public void Add(T item)
{
if (this.items.IsReadOnly)
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
this.InsertItem(this.items.Count, item);
}
protected virtual void InsertItem(int index, T item)
{
this.items.Insert(index, item);
}
}
公共类集合:IList、ICollection、IList、ICollection、IReadOnlyList、IReadOnlyCollection、IEnumerable、IEnumerable
{
公共作废新增(T项)
{
如果(此.items.IsReadOnly)
ThrowHelper.ThrownNotSupportedException(ExceptionResource.NotSupported\u ReadOnlyCollection);
this.InsertItem(this.items.Count,item);
}
受保护的虚拟void插入项(int索引,T项)
{
本.项目.插入(索引,项目);
}
}
InsertItem是一个虚拟方法,在ObservaleCollection中被重写
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
protected override void InsertItem(int index, T item)
{
this.CheckReentrancy();
base.InsertItem(index, item);
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Add, (object) item, index);
}
}
公共类ObservableCollection:Collection、INotifyCollectionChanged、INotifyPropertyChanged
{
受保护的覆盖无效插入项(int索引,T项)
{
this.CheckReentrancy();
基本插入项(索引,项目);
本.不动产变更(“计数”);
本.OnPropertyChanged(“第[]项”);
this.OnCollectionChanged(NotifyCollectionChangedAction.Add,(对象)项,索引);
}
}
ObservableCollection