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;是如何返回值的呢?????各位热心的朋友帮帮我!!

解决方案 »

  1.   

    1. main是静态的方法,在静态方法中只能调用静态方法使用静态属性。2. 是的。3. 你传入的ArrayList有具体的实现,返回的是你所传入对象的内部实现。
      

  2.   

    1>.类我们都是先实例化再来引用的,而静态函数可能看成一个实例化了的函数.public int ReturnCount(ICollection col)只说明这是个公共类而已.
    2>.可以,因为: 接口 = ICollection.
    3>.COUNT是集合中的公有属性,你定义的是public int ReturnCount(ICollection col),返回的当然是INT型的值.
      

  3.   

    (1)这段代码提示错误“非静态的字段、方法或属性“ConsoleApplication1.Class1.ReturnCount(System.Collections.ICollection)”要求对象引用” 
    改为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中,因为接口是必须要重载的。
      

  4.   

    通过接口来操作对象,有点像com
      

  5.   

    static main只能掉static 的方法