static void Main(string[] args)
{
ArrayList ar=new ArrayList();
int count=ReturnCount(ar);
}
public int ReturnCount(ICollection col)
{
return col.Count;
}(1)这段代码提示错误“非静态的字段、方法或属性“ConsoleApplication1.Class1.ReturnCount(System.Collections.ICollection)”要求对象引用”
改为public static int ReturnCount(ICollection col) 运行就正常了!
我不明白为什么啊??(2)ReturnCount里的参数是ICollection,而程序传递的是ar是ArrayList 类型,这样可以??
是不是因为ArrayList实现了ICollection接口,所以就相当于ICollection的子类,所以可以??(3)ArrayList实现了ICollection接口,可是ICollection接口只是有Count方法的定义,并没有它的具体实现,return col.Count;是如何返回值的呢?????各位热心的朋友帮帮我!!
{
ArrayList ar=new ArrayList();
int count=ReturnCount(ar);
}
public int ReturnCount(ICollection col)
{
return col.Count;
}(1)这段代码提示错误“非静态的字段、方法或属性“ConsoleApplication1.Class1.ReturnCount(System.Collections.ICollection)”要求对象引用”
改为public static int ReturnCount(ICollection col) 运行就正常了!
我不明白为什么啊??(2)ReturnCount里的参数是ICollection,而程序传递的是ar是ArrayList 类型,这样可以??
是不是因为ArrayList实现了ICollection接口,所以就相当于ICollection的子类,所以可以??(3)ArrayList实现了ICollection接口,可是ICollection接口只是有Count方法的定义,并没有它的具体实现,return col.Count;是如何返回值的呢?????各位热心的朋友帮帮我!!
2>.可以,因为: 接口 = ICollection.
3>.COUNT是集合中的公有属性,你定义的是public int ReturnCount(ICollection col),返回的当然是INT型的值.
改为public static int ReturnCount(ICollection col) 运行就正常了!
我不明白为什么啊??
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
非静态的方法不能直接调用,这样也可以实现:
ArrayList ar=new ArrayList();类名 objclass = new 类名();
int count = objclass.ReturnCount(ar);
(2)ReturnCount里的参数是ICollection,而程序传递的是ar是ArrayList 类型,这样可以??
是不是因为ArrayList实现了ICollection接口,所以就相当于ICollection的子类,所以可以??
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
可以。
(3)ArrayList实现了ICollection接口,可是ICollection接口只是有Count方法的定义,并没有它的具体实现,return col.Count;是如何返回值的呢?????
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
具体的实现在ArrayList中,因为接口是必须要重载的。