·using System;
using System.Collections;
public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!"); // Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( " Count: {0}", myAL.Count );
Console.WriteLine( " Capacity: {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
} public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
}}
在函数定义时PrintValues中的参数类型为接口类型 Ienumerable, 但是在调用 PrintValues( myAL ),参数myAL为 ArrayList。这个怎么理解? 初学者对接口,还有类的关系总是很迷茫,求大侠解释,发文。谢谢
using System.Collections;
public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!"); // Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( " Count: {0}", myAL.Count );
Console.WriteLine( " Capacity: {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
} public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
}}
在函数定义时PrintValues中的参数类型为接口类型 Ienumerable, 但是在调用 PrintValues( myAL ),参数myAL为 ArrayList。这个怎么理解? 初学者对接口,还有类的关系总是很迷茫,求大侠解释,发文。谢谢
就好比你定义一个objec的参数,传任何东西都可以。
因为一切类型都派生自objec。接口就是定义一些列规范,让子类必须实现。定义接口,传接口子类。也就是父类引用指向子类对象。这样的例子很多。Object obj=new MyClass();