Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法重写/隐藏C语言中的行为#_C#_Inheritance_Polymorphism_Overriding - Fatal编程技术网

C# 方法重写/隐藏C语言中的行为#

C# 方法重写/隐藏C语言中的行为#,c#,inheritance,polymorphism,overriding,C#,Inheritance,Polymorphism,Overriding,我对C#的理解是,子类不能重写父类的方法实现,除非该方法被标记为虚拟。如果子类声明了一个与父类方法同名的方法,而父类方法未标记为虚拟,则它只是隐藏该方法,这样,如果从父类类型的引用调用该方法,它将调用父方法,如果从子类类型的引用调用该方法,它将调用子类方法。然而,我在C#library中发现了一种情况,似乎破坏了这种行为 集合声明了一个方法public void Add(T项)。 此方法不是虚拟的,因此子类中的实现不应重写其行为。然而,下面的测试产生了一个矛盾的结果 公共无效测试() { Obs

我对C#的理解是,子类不能重写父类的方法实现,除非该方法被标记为虚拟。如果子类声明了一个与父类方法同名的方法,而父类方法未标记为虚拟,则它只是隐藏该方法,这样,如果从父类类型的引用调用该方法,它将调用父方法,如果从子类类型的引用调用该方法,它将调用子类方法。然而,我在C#library中发现了一种情况,似乎破坏了这种行为

集合
声明了一个方法
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是从集合派生的,通知事件将在分配了ObservableCollection类中定义的处理程序的位置进行处理,Add Method of Collection call Insert方法是虚拟的,在ObservableCollection类中被重写,在被重写的方法中调用事件处理程序。

ObservableCollection是从集合派生的,通知事件将在分配了在ObservaleCollection类中,集合的Add方法调用Insert方法,该方法是虚拟的,在ObservaleCollection类中被重写,在被重写的方法中调用事件处理程序。

Ok,这很有意义。谢谢好的,这是有道理的。谢谢