C# 接口继承在这里是正确的吗?为什么它不起作用?
我正在实现某种类型(C# 接口继承在这里是正确的吗?为什么它不起作用?,c#,inheritance,interface,C#,Inheritance,Interface,我正在实现某种类型(MyType,在下面的示例中),它具有集合属性。在MyType内部,我并不在乎这是什么样的收藏。我唯一关心的是它实现了IEnumerable和INotifyCollectionChanged。我将如何实现具有这些限制的集合属性 以下是我尝试过的: 我创建了一个新界面: interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {}
MyType
,在下面的示例中),它具有集合
属性。在MyType
内部,我并不在乎这是什么样的收藏。我唯一关心的是它实现了IEnumerable
和INotifyCollectionChanged
。我将如何实现具有这些限制的集合
属性
以下是我尝试过的:
我创建了一个新界面:
interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {}
您的类需要实现INotifyEnumerableCollectionChanged
类MyCollection:InotifyNumerableCollectionChanged。
IEnumerable,INotifyCollectionChanged
{
IEnumerator IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
公共事件通知CollectionChangedEventHandler CollectionChanged;
}
这是必需的,因为您使用的变量(
\u collection
)的类型为InotifyNumerableCollectionChanged
。但是,您的类-MyCollectionChanged
未实现此接口,因此无法将其指定为变量的引用。您的类需要实现INotifyEnumerableCollectionChanged
类MyCollection:InotifyNumerableCollectionChanged。
IEnumerable,INotifyCollectionChanged
{
IEnumerator IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
公共事件通知CollectionChangedEventHandler CollectionChanged;
}
这是必需的,因为您使用的变量(
\u collection
)的类型为InotifyNumerableCollectionChanged
。但是,您的类-MyCollectionChanged
未实现此接口,因此无法将其指定为变量的引用。您创建了一个新接口,但未使用它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace InterfaceInheranceTest
{
class Program
{
interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {}
class MyCollection : INotifyEnumerableCollectionChanged<String> // Use your interface
{
IEnumerator<String> IEnumerable<String>.GetEnumerator()
{ throw new NotImplementedException(); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{ throw new NotImplementedException(); }
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
class MyType
{
private INotifyEnumerableCollectionChanged<String> _Collection;
public INotifyEnumerableCollectionChanged<String> Collection
{
get { return _Collection; }
set
{
_Collection = value;
_Collection.CollectionChanged += new NotifyCollectionChangedEventHandler(_Collection_CollectionChanged);
foreach (var item in _Collection)
{
Console.WriteLine(item);
}
}
}
void _Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{ throw new NotImplementedException(); }
}
static void Main(string[] args)
{
var collection = new MyCollection();
var type = new MyType();
type.Collection = collection; // compiler doesn't like this!
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Collections.Specialized;
命名空间接口InheranceTest
{
班级计划
{
接口INotifyNumerableCollectionChanged:IEnumerable,INotifyCollectionChanged{}
类MyCollection:InotifyNumerableCollectionChanged//使用您的接口
{
IEnumerator IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
公共事件通知CollectionChangedEventHandler CollectionChanged;
}
类MyType
{
私有INotifyEnumerableCollectionChanged\u集合;
public INotifyEnumerableCollectionChanged集合
{
获取{return\u Collection;}
设置
{
_收集=价值;
_Collection.CollectionChanged+=新通知collectionchangedventhandler(\u Collection\u CollectionChanged);
foreach(集合中的var项)
{
控制台写入线(项目);
}
}
}
void\u Collection\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{抛出新的NotImplementedException();}
}
静态void Main(字符串[]参数)
{
var collection=new MyCollection();
var type=new MyType();
type.Collection=Collection;//编译器不喜欢这样!
}
}
}
定义相同的方法并不意味着它是相同的类型。您创建了一个新接口,但没有使用它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace InterfaceInheranceTest
{
class Program
{
interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {}
class MyCollection : INotifyEnumerableCollectionChanged<String> // Use your interface
{
IEnumerator<String> IEnumerable<String>.GetEnumerator()
{ throw new NotImplementedException(); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{ throw new NotImplementedException(); }
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
class MyType
{
private INotifyEnumerableCollectionChanged<String> _Collection;
public INotifyEnumerableCollectionChanged<String> Collection
{
get { return _Collection; }
set
{
_Collection = value;
_Collection.CollectionChanged += new NotifyCollectionChangedEventHandler(_Collection_CollectionChanged);
foreach (var item in _Collection)
{
Console.WriteLine(item);
}
}
}
void _Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{ throw new NotImplementedException(); }
}
static void Main(string[] args)
{
var collection = new MyCollection();
var type = new MyType();
type.Collection = collection; // compiler doesn't like this!
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Collections.Specialized;
命名空间接口InheranceTest
{
班级计划
{
接口INotifyNumerableCollectionChanged:IEnumerable,INotifyCollectionChanged{}
类MyCollection:InotifyNumerableCollectionChanged//使用您的接口
{
IEnumerator IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{抛出新的NotImplementedException();}
公共事件通知CollectionChangedEventHandler CollectionChanged;
}
类MyType
{
私有INotifyEnumerableCollectionChanged\u集合;
public INotifyEnumerableCollectionChanged集合
{
获取{return\u Collection;}
设置
{
_收集=价值;
_Collection.CollectionChanged+=新通知collectionchangedventhandler(\u Collection\u CollectionChanged);
foreach(集合中的var项)
{
控制台写入线(项目);
}
}
}
void\u Collection\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{抛出新的NotImplementedException();}
}
静态void Main(字符串[]参数)
{
var collection=new MyCollection();
var type=new MyType();
type.Collection=Collection;//编译器不喜欢这样!
}
}
}
定义相同的方法并不意味着它是相同的类型。Ok编译。但为什么需要这样做呢?它已经实现了
IEnumerable
和INotifyCollectionChangedusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace InterfaceInheranceTest
{
class Program
{
interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {}
class MyCollection : INotifyEnumerableCollectionChanged<String> // Use your interface
{
IEnumerator<String> IEnumerable<String>.GetEnumerator()
{ throw new NotImplementedException(); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{ throw new NotImplementedException(); }
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
class MyType
{
private INotifyEnumerableCollectionChanged<String> _Collection;
public INotifyEnumerableCollectionChanged<String> Collection
{
get { return _Collection; }
set
{
_Collection = value;
_Collection.CollectionChanged += new NotifyCollectionChangedEventHandler(_Collection_CollectionChanged);
foreach (var item in _Collection)
{
Console.WriteLine(item);
}
}
}
void _Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{ throw new NotImplementedException(); }
}
static void Main(string[] args)
{
var collection = new MyCollection();
var type = new MyType();
type.Collection = collection; // compiler doesn't like this!
}
}
}