集合

1:在集合中输出一个类的某个属性值

namespace fanxing

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList a = new ArrayList();



            a.Add(new Student("Tang",12));   //添加两个对象

            a.Add(new Student("Zhang",13));



            for (int i = 0; i < a.Count; i++)

            {

                Console.WriteLine("姓名为:{0},  年龄为:{1}",((Student)a[i]).StudentName,((Student)a[i]).Age);   //将集合 强制 转化为 Student类,后输出其属性 StudentName

            }

        }



        public class Student

        {

            private string studentName;

            private int age;

            public string StudentName

            {

                get

                {

                    return studentName;   //一定要有这句

                }

            }

            public int Age

            {

                get { return age; }

            }

            public Student(string sStudentName,int aAge)

            {

                this.studentName = sStudentName;

                this.age = aAge;

            }

        }

    }

}

结果:

 


 

2:集合 与 数组的比较

首先有个类 Animal

 1 public  class Animal

 2     {

 3         private string name;  //私有成员不可再子类中访问

 4 

 5         public string Name

 6         {

 7             get { return name; }

 8             set { name = value; }

 9 

10         }

11         public Animal()

12         {

13             Name = "the animal with no name";

14         }

15         public Animal(string name)

16         {

17             Name = name;

18         }

19 

20         public void Feed()

21         {

22             Console.WriteLine("[ {0} ] has been Feed!", Name);

23         }

24 

25     }

 

然后有个类 Cow 继承与 Animal

 1     public class Cow : Animal

 2     {

 3         public void Milk()

 4         {

 5             Console.WriteLine("[ {0} ] has been Milked!", Name);

 6         }

 7         public Cow(string newName) : base(newName)  //此方法用于继承父类的构造函数

 8         {

 9 

10         }

11     }

还有个类 Chiken 继承于 Animal

 1     public class Chicken : Animal

 2     {

 3         public void LayEgg()

 4         {

 5             Console.WriteLine("[ {0} ] has been LayEgg", Name);

 6         }

 7 

 8         public Chicken(string newName)   //继承父类的构造函数参数

 9             : base(newName)

10         {

11         }

12     }

 

Main()函数中:

 1 class Program

 2     {

 3         static void Main(string[] args)

 4         {

 5 

 6             Console.WriteLine("创建一个数组来保存类:");

 7             Animal[] animalArray = new Animal[3];  //数组只能保存个数确定的对象实例

 8             Animal an1 = new Animal("test1");

 9             Cow cow1 = new Cow("test2");

10             Chicken ch1 = new Chicken("test3");

11 

12             animalArray[0] = an1;

13             animalArray[1] = cow1;

14             animalArray[2] = ch1;

15 

16             //分别输出数组中的值:

17             for(int i=0; i<animalArray.Length;i++)

18             {

19                 Console.WriteLine("新对象({0})加入到数组中,Name = {1}",animalArray[i],animalArray[i].Name);

20             }

21             Console.WriteLine("数组长度为:{0}", animalArray.Length);  //数组长度使用 Length属性

22 

23             Console.WriteLine("调用相应的方法:");

24             animalArray[0].Feed();

25             //数组的类型是抽象类型Animal,因此不能直接调用由派生类提供的方法,而必须使用数据类型转换

26             ((Cow)animalArray[1]).Milk(); 

27             ((Chicken)animalArray[2]).LayEgg();

28 

29             Console.WriteLine("-----------------------------华丽的分割线--------------------------------");

30 

31 

32             Console.WriteLine("创建一个集合类来保存类:");

33             ArrayList al = new ArrayList();  //集合类可以保存N个对象实例

34             al.Add(new Animal("test11"));

35             al.Add(new Cow("test22"));

36             al.Add(new Chicken("test33"));

37 

38             // 方法一

39             //此处必须为 Animal   如果会用Var类型,则 item无法获取Name属性; Cow类型也不行

40             foreach (Animal item in al)  

41             {

42                 Console.WriteLine("新对象({0})加入到数组中,Name={1}", item,item.Name);

43             }

44             //方法二

45             //for (int i = 0; i < al.Count; i++)

46             //{

47             //    Console.WriteLine("新对象({0})加入到数组中,Name={1}", al[i],((Animal)al[i]).Name);  //注意这里

48             //}

49             Console.WriteLine("集合长度为:{0}", al.Count);  //集合的长度使用 Count属性

50 

51             Console.WriteLine("调用相应的方法:");

52             ((Animal)al[0]).Feed();

53             ((Cow)al[1]).Milk();

54             ((Chicken)al[2]).LayEgg();

55 

56             Console.WriteLine();

57             Console.WriteLine("集合中删除对象:");

58             al.Remove(al[0]);  //移除一个指定的对象

59             al.RemoveAt(1);

60             foreach (Animal item in al)

61             {

62                 Console.WriteLine("新对象({0})加入到数组中,Name={1}", item, item.Name);

63             }

64 

65         }

分析:  分别使用 数组和集合两种方式演示了对对象的添加,删除,访问属性等操作。

结果:

集合

 

 

3: 泛型集合中的对象的访问

Program函数中:

 1        Console.WriteLine("-----------------------------华丽的分割线--------------------------------");

 2 

 3             Console.WriteLine("List<T>");

 4             List<Animal> list = new List<Animal>();

 5             list.Add(new Animal("testA")); //向泛型集合添加成员

 6             list.Add(new Cow("testB"));

 7             list.Add(new Chicken("testC"));

 8             foreach (Animal item in list)  //遍历泛型集合

 9             {

10                 Console.WriteLine("类型:{0}, Name = {1}",item,item.Name);

11             }

12             list[0].Feed();

13             ((Cow)list[1]).Milk();

14             ((Chicken)list[2]).LayEgg();

15 

16             Console.WriteLine();

17             Console.WriteLine("Dictionary<Key,value>");

18 

19             Dictionary<string, Animal> dic = new Dictionary<string, Animal>();

20             dic.Add("a1", new Animal("testA")); //向 字典中添加成员

21             dic.Add("a2", new Cow("testB"));

22             dic.Add("a3", new Chicken("testC"));

23             //分别遍历 Key 值  和 Value 值

24             foreach (string item in dic.Keys)  //遍历字典的 key值

25             {

26                 Console.WriteLine("Key = {0}",item);

27             }

28             foreach (Animal item in dic.Values)  //遍历字典的 values 值

29             {

30                 Console.WriteLine("Value = {0}",item.Name);

31             }

32             Console.WriteLine("分别调用各个类的方法:");

33             dic["a1"].Feed();

34             ((Cow)dic["a2"]).Milk();

35             ((Chicken)dic["a3"]).LayEgg();

 注意调用方法的时候需要 转化为相应的类型才行! 33-15行所示!

结果:

集合

 

源码下载

你可能感兴趣的:(集合)