近日看书。了解到CollectionBase的List属性是获得表示集合实例的Ilist实例。学习到以下类式代码。有疑问。请高手解答:CollectionBase 为一个抽象类。当我的类要继承这个类的时候。有如下代码:
public A
{ //....}public Myclass:CollectionBase
{ //...... public virtual int Add(A a)
{
return List.Add(a);
} public virtual int IndexOf(A a)
{
return List.IndexOf(a);
} //..
}为什么当我实现Add方法时。可以直接调用List属性返回的实例的方法?List是返回一个Ilist接口的实例。但是Ilist并没有实现Add等方法,只是给出了定义。请问此处如何实现的?因为同样的属性Innerlist是返回ArrayList的实例,但是ArrayList类中已经实现了Add等方法啊。谢谢大家!!
public A
{ //....}public Myclass:CollectionBase
{ //...... public virtual int Add(A a)
{
return List.Add(a);
} public virtual int IndexOf(A a)
{
return List.IndexOf(a);
} //..
}为什么当我实现Add方法时。可以直接调用List属性返回的实例的方法?List是返回一个Ilist接口的实例。但是Ilist并没有实现Add等方法,只是给出了定义。请问此处如何实现的?因为同样的属性Innerlist是返回ArrayList的实例,但是ArrayList类中已经实现了Add等方法啊。谢谢大家!!
解决方案 »
- 请问类似安装程序点击next按钮进入到下一个窗体的效果,如何实现
- 小弟应老师要求用C#做个学生管理系统,可怎么也练不上数据库,望各位高手帮帮忙看下是哪里的问题!
- 这是我编的C#,判断一个数是不是素数的程序,帮忙看看对不对
- 关于数据库和DataGrid的问题
- .net 时有没有像 Delphi 里的 ActionList 一样的控件喔?
- 请帮忙把一段vb语句改成C#的语句,在线等
- 进行视频压缩之后用udp传输。(高分求代码!)
- 页面的定位
- 关于网页中的 TreeView ,我想在树显示时 根接点的链接已经被打开
- 请问谁有Antechinus C# Editor的注册码?
- 领导叫我处理这样一个问题:
- VisualStudioNET安装问题
using System;
using System.Collections;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 ) );
} protected override void OnInsert( int index, Object value ) {
// Insert additional code to be run only when inserting values.
} protected override void OnRemove( int index, Object value ) {
// Insert additional code to be run only when removing values.
} protected override void OnSet( int index, Object oldValue, Object newValue ) {
// Insert additional code to be run only when setting values.
} protected override void OnValidate( Object value ) {
if ( value.GetType() != typeof(System.Int16) )
throw new ArgumentException( "value must be of type Int16.", "value" );
}}
IList.Add
int Add (
Object value
)
IList.Add方法实现了的 你把问题搞复杂了
IList中的方法都为公共方法 都已经实现了的
名称 说明
Add 将某项添加到 IList 中。
Clear 从 IList 中移除所有项。
Contains 确定 IList 是否包含特定值。
IndexOf 确定 IList 中特定项的索引。
Insert 将一个项插入指定索引处的 IList。
Remove 从 IList 中移除特定对象的第一个匹配项。
RemoveAt 移除指定索引处的 IList 项。
{
return( List.Add( value ) );
}晕,和我举例一样啊。楼上的这个例子也是继承CollectionBase类。也是通过List属性返回Ilist实例进行Add操作。可是没解答我的问题啊。为何此处可以直接使用Ilist的实例进行Add操作?Ilist并没有实现这样的操作,接口只是给出了定义,由继承类进行实现。但是CollectionBase也没有实现诸如Add等方法的实现,请问此处是如何实现方法的?
.Net中真正在runtime实现的语言数组只有System.Array,但.Net还是具有一个很大的集合框架,它由System.Collections和System.Collections.Specialized 命名空间组成,里面包含了高级和特殊目的特性的集合,用与代替System.Array,在使用这些高级的替代集合类型的时候,从根本出发还是为了性能.在真正的对象化开发项目中,我们通常会将常用的业务实体抽象为特定的类.通常使用System.Array的时候能达到语法简捷,但却牺牲了速度,而MS定义这些集合正是为了更接近它所代表的建模实体,自然在保持语法简捷的同时也能保证一定的速度.其实 你读2边再想想
你的意思是:
IList中的方法都为公共方法 都已经实现了的
但是接口和类一样都可以是公共方法。不同的是接口没有对方法进行实现此处的Ilist是一个接口啊,并没有对方法进行实现啊。System.Collection定义了一些接口。确定了所有集合类的通用功能。就象ArrayList一样。是实现了ICollection,IList,IEnumerable,ICloneable
ArrayList list=new ArrayList ();bool IList.IsReadOnly {
get { return list.IsReadOnly; }
}}
ArrayList list=new ArrayList ();bool IList.IsReadOnly {
get { return list.IsReadOnly; }
}}
MSDN中CollectionBase内部没有实现IsReadOnly方法啊。此方法是抽象的。bool IList.IsReadOnly {
get { return list.IsReadOnly; }
}
还有这个是什么意思啊?继承Ilist定义的方法?而且你在CollectionBase自己的类里调用自己的属性list.IsReadOnly这
2,也可以反汇编看用ildasm
{
void test();
}class Myclass1:Imy
{
public virtual void test()
{
Console.WriteLine("This is Myclass1 1#");
}
}class Myclass2:Myclass1,Imy
{
public override void test()
{
Console.WriteLine("This is Myclass2 1#");
}
}class Demo
{
public static void Main()
{
//Myclass1 a=new Myclass1();
Myclass2 a=new Myclass2();
Myclass1 b=a;
Imy c=(Imy)b;
c.test();
Console.Read();
}
}
如何实现4次?可以府上你的代码吗?