public class MyItem { ... public void Insert( int index, MyItem item ) ... // 实现 Ilist 的 Insert 方法 void Insert( int index, obejct item ) { Insert(index, (MyItem)item); } //你自己添加一个Insert方法 public void Insert( int index, MyItem item ) { ... }
★ To ccat(智拙): void Insert( int index, obejct item ) 不加任何访问限定符编译出错:没有实现 Insert。你的意思是通过重载和访问限制,只公开方法重载版本,但是实现接口方法不用 public 编译就通不过。能不能把你的代码贴上来呢?谢谢。
★ To henryfan1(每天好心情(*_*)) : tab 键实现接口定义的确在 2002 中没有。★ To ccat(智拙): 如果不实现 IList 接口,改为继承 CollectionBase 也能实现。但是对: public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager 的 Insert 方法 public void Insert(int index, ListItem item) 的实现不解,希望能够指点迷津。
public class Int16Collection : CollectionBase { public Int16 this[ int index ] { get { return( (Int16) List[index] ); } set { List[index] = value; } } public int Add( Int16 value ) { return( List.Add( value ) ); } public int IndexOf( Int16 value ) { return( List.IndexOf( value ) ); } public void Insert( int index, Int16 value ) { List.Insert( index, value ); } public void Remove( Int16 value ) { List.Remove( value ); } public bool Contains( Int16 value ) { // If value is not of type Int16, this will return false. return( List.Contains( value ) ); } } 这是MSDN的例子,你可以把Int16改成你的类型
public class MyItem { ... public void Insert( int index, MyItem item ) ... void Insert( int index, obejct item ) { Insert(index, (MyItem)item); }学了两手
public class MyItem
{
...
public void Insert( int index, MyItem item )
... void Insert( int index, obejct item )
{
Insert(index, (MyItem)item);
}
在IDE里写完public class MyItem : IList以后停一下,应该会提示你按TAB键自动完成接口代码。
---------
1。在类视图中,点击类的接口右键,然后点击添加-实现接口。
2。在编写类时,承继接口后停一下,按TAB键,类就自动添加其接口所有成员代码。
> 2。在编写类时,承继接口后停一下,按TAB键,类就自动添加其接口所有成员代码。停一下然后按 Tab 键没有任何反应呀?我用的是 Visual Studio.net 2002 English
public class MyItem
{
...
public void Insert( int index, MyItem item )
... void Insert( int index, obejct item )
{
Insert(index, (MyItem)item);
}这样 Insert 被调用时的参数类型还是 object 呀,看看这个类 ListItemCollection:
public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
它的方法 public void Insert(int index, ListItem item) 是怎么实现的呢?
{
...
public void Insert( int index, MyItem item )
... // 实现 Ilist 的 Insert 方法
void Insert( int index, obejct item )
{
Insert(index, (MyItem)item);
}
//你自己添加一个Insert方法
public void Insert( int index, MyItem item )
{
...
}
void Insert( int index, obejct item )
不加任何访问限定符编译出错:没有实现 Insert。你的意思是通过重载和访问限制,只公开方法重载版本,但是实现接口方法不用 public 编译就通不过。能不能把你的代码贴上来呢?谢谢。
tab 键实现接口定义的确在 2002 中没有。★ To ccat(智拙):
如果不实现 IList 接口,改为继承 CollectionBase 也能实现。但是对:
public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
的 Insert 方法 public void Insert(int index, ListItem item) 的实现不解,希望能够指点迷津。
get {
return( (Int16) List[index] );
}
set {
List[index] = value;
}
} public int Add( Int16 value ) {
return( List.Add( value ) );
} public int IndexOf( Int16 value ) {
return( List.IndexOf( value ) );
} public void Insert( int index, Int16 value ) {
List.Insert( index, value );
} public void Remove( Int16 value ) {
List.Remove( value );
} public bool Contains( Int16 value ) {
// If value is not of type Int16, this will return false.
return( List.Contains( value ) );
}
}
这是MSDN的例子,你可以把Int16改成你的类型
{
...
public void Insert( int index, MyItem item )
... void Insert( int index, obejct item )
{
Insert(index, (MyItem)item);
}学了两手
确实是我写错了,不加限定符的那个应该是IList.Insert,也就是说,要加上完全引用名。详细的讲解请参见《Microsoft.NET 框架程序设计(修订版)》15.5,338页至343页的内容。