1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using System.Collections; 8 9 namespace day01 10 { 11 class Class24 12 { 13 static void Main(string[] args) 14 { 15 ArrayList list = new ArrayList(); 16 list.Add(new Student("zhangsan", 23)); 17 list.Add(new Student("lisi", 24)); 18 list.Add(new Student("wangwu", 25)); 19 20 foreach(Student temp in list) 21 { 22 Console.WriteLine(temp.ToString()); 23 } 24 25 Console.WriteLine(); 26 //其实调用的对象是Object类型的 因为重写了Object的ToString()方法 所以调用时有多态的存在 27 Console.WriteLine(list[2].ToString()); 28 //验证了返回的对象类型为Object类型 要调用方法 需要强制类型转换才行 29 ((Student)list[2]).GetInfo(); 30 Console.ReadKey(); 31 } 32 } 33 public class Student 34 { 35 private string name; 36 private int age; 37 public string Name { get; set; } 38 public int Age { get; set; } 39 public Student() 40 { 41 42 } 43 public Student(string name,int age) 44 { 45 Name = name; 46 Age = age; 47 } 48 public override string ToString() 49 { 50 return "name: " + Name + " age: " + Age; 51 } 52 public void GetInfo() 53 { 54 Console.WriteLine("name is {0} age is {1}", Name, Age); 55 } 56 } 57 }
C#集合C#基础学习
猜你喜欢
转载自www.cnblogs.com/littlelittleprince/p/10668883.html
今日推荐
周排行