代码图理解复杂代码

类图

1.抽象动物类Animal

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Demo2
  7. {
  8. public abstract class Animal
  9. {
  10. protected string name;
  11. public string Name
  12. {
  13. get
  14. {
  15. return name;
  16. }
  17. set
  18. {
  19. name = value;
  20. }
  21. }
  22. public Animal()
  23. {
  24. name = "The animal with no name";
  25. }
  26. public Animal(string newName)
  27. {
  28. name = newName;
  29. }
  30. public void Feed()
  31. {
  32. Console.WriteLine("{0} has been fed.",name);
  33. }
  34. }
  35. }

2.牛类Cow

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Demo2
  7. {
  8. public class Cow:Animal
  9. {
  10. public void Milk()
  11. {
  12. Console.WriteLine("{0} has been milked.",name);
  13. }
  14. public Cow(string newName):base(newName) // 继承了父类
  15. {
  16. }
  17. }
  18. }

3.鸡类Chicken

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Demo2
  7. {
  8. public class Chicken:Animal
  9. {
  10. public void LayEgg()
  11. {
  12. Console.WriteLine("{0} has laid an egg.",name);
  13. }
  14. public Chicken(string newName) :base(newName)
  15. {
  16. }
  17. }
  18. }

4.主类Program,用到了数组和集合Array,ArrayList

  1. using System;
  2. using System.Collections; // 集合
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Demo2
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // 数组的形式
  14. Console.WriteLine("Create an array type collection of Animal objects and use it:");
  15. Animal[] animalArray = new Animal[2];
  16. Cow myCow1 = new Cow("Deirdre");
  17. Chicken myChicken1 = new Chicken("Ken");
  18. animalArray[0] = myCow1;
  19. animalArray[1] = myChicken1;
  20. foreach (Animal myAnimal in animalArray)
  21. {
  22. Console.WriteLine("New {0} object added to Array collection,Name = {1}", myAnimal.ToString(),myAnimal.Name);
  23. }
  24. Console.WriteLine("Array collection contains {0} objects.",animalArray.Length);
  25. animalArray[0].Feed();
  26. ((Chicken)animalArray[1]).LayEgg();
  27. // 集合的形式
  28. Console.WriteLine("Create an ArrayList type collection of Animal objects and use it:");
  29. ArrayList animalArrayList = new ArrayList();
  30. Cow myCow2 = new Cow("Hayley");
  31. animalArrayList.Add(myCow2);
  32. animalArrayList.Add(new Chicken("Roy"));
  33. foreach (Animal myAnimal in animalArrayList)
  34. {
  35. Console.WriteLine("New {0} object added to ArrayList collection,Name = {1}", myAnimal.ToString(), myAnimal.Name);
  36. }
  37. Console.WriteLine("Array collection contains {0} objects.", animalArrayList.Count); // 注意这里是Count
  38. ((Animal)animalArrayList[0]).Feed();
  39. ((Chicken)animalArrayList[1]).LayEgg();
  40. Console.ReadKey();
  41. }
  42. }
  43. }

两种效果差不多,细节略有区别!

再看下面,改造

定义Animals类,不需要通过ArrayList了。Animals就是ArrayList。

Animals.cs

  1. using System;
  2. using System.Collections; // 集合
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Demo2
  8. {
  9. public class Animals : CollectionBase
  10. {
  11. public void Add(Animal newAnimal)
  12. {
  13. List.Add(newAnimal);
  14. }
  15. public void Remove(Animal newAnimal)
  16. {
  17. List.Remove(newAnimal);
  18. }
  19. public Animal this[int animalIndex]
  20. {
  21. get
  22. {
  23. return (Animal)List[animalIndex];
  24. }
  25. set
  26. {
  27. List[animalIndex] = value;
  28. }
  29. }
  30. }
  31. }

使用Animals

  1. using System;
  2. using System.Collections; // 集合
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Demo2
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Animals animalCollection = new Animals();
  14. animalCollection.Add(new Cow("Jack"));
  15. animalCollection.Add(new Chicken("Vera"));
  16. foreach(Animal myAnimal in animalCollection)
  17. {
  18. myAnimal.Feed();
  19. }
  20. Console.ReadKey();
  21. }
  22. }
  23. }

Array可以包涵基本类型和对象类型,ArrayList只能包涵对象类型。

Array大小是固定的,ArrayList的大小是动态变化的。

ArrayList提供了更多的方法和特性,比如:addAll(),removeAll(),iterator()等等。

对于基本数据类型,集合使用自动装箱来减少编码的工作量。但是当处理固定大小的基本数据类型的时候这种方式相对比较慢。

方法论:

实践加理论!多查查相关的资料,总结一下!

Array与ArrayList的更多相关文章

  1. C#中数组Array、ArrayList、泛型List<T>的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  2. Array和ArrayList互相转换

    class Order{ public string orderId; public string orderName; public decimal orderPrice; } public cla ...

  3. Array和ArrayList的异同点【转】

    相信数组是大家在编程最常使用的,不论任何语言都存在数组这样的数据结构,由于C#语言是完全面向对象的,所以在C#中的数组也是对象,实际上就是Array类的实例,Array类的使用可以说是使用最频繁的,只 ...

  4. Array和ArrayList的区别与联系

    博主今天去了一个java的实习面试,发现有好多java最基础的数据结构对于博主来说反而感到陌生,在面试官问一些常见的例如HashMap这样的数据结构,博主能回答的头头是道,但是在问到Array和Arr ...

  5. Array和ArrayList的异同点

    Array和ArrayList的异同点 1.不同点: (1)Array只能存储同构的对象, ArrayList可以存储异构的对象 (2)在CLR托管对中的存放方式中,Array是始终是连续存放的, A ...

  6. C# array与arraylist区别及获取sql字段名

    array与arraylist的区别: 1.  Array 的容量是固定的,而 ArrayList 的容量是根据需要自动扩展的.如果更改了 ArrayList.Capacity 属性的值,则自动进行内 ...

  7. Array和ArrayList有什么区别?

    Array和ArrayList的区别: 1.Array可以包含基本数据类型和对象类型,而ArrayList只能包含对象类型 2.Array有固定的大小,而ArrayList是动态变化的. 3.Arra ...

  8. C#数组之 []、List、Array、ArrayList应用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. C#中[] 、List、Array、ArrayList等数据结构的差异简述

    [] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList 是针对任意类型.任意长度的. Array 和 ArrayLis ...

  10. Java面试题之Array和ArrayList的区别

    Array和ArrayList的区别: 1.Array类型的变量在声明的同时必须进行实例化(至少得初花数组的大小),而ArrayList可以只是先声明: 2.Array始终是连续存放的:而ArrayL ...

随机推荐

  1. Python有了asyncio和aiohttp在爬虫这类型IO任务中多线程/多进程还有存在的必要吗?

    最近正在学习Python中的异步编程,看了一些博客后做了一些小测验:对比asyncio+aiohttp的爬虫和asyncio+aiohttp+concurrent.futures(线程池/进程池)在效 ...

  2. php基础:define()定义常数函数

    define(); 常量类似变量,不同之处在于: 在设定以后,常量的值无法更改 常量名不需要开头的美元符号 ($) 作用域不影响对常量的访问 常量值只能是字符串或数字 <?php define( ...

  3. fill,fill-n,memset的区别

    这里在网上搜集归纳了一个总结 memset函数 按照字节填充某字符 在头文件<string.h>中 因为memset函数按照字节填充,所以一般memset只能用来填充char型数组,(因为 ...

  4. php修改限制上传文件大小

    win下:     1.编辑 php.ini:修改在 php5 下文件大小的限制     找到:file_uploads=On  允许 HTTP 文件上传     找到:max_execution_t ...

  5. python BeautifulSoup 获取页面多个子节点中的各个节点的内容

    页面html格式为 <tr bgcolor="#7bb5de"><td style="border-bottom: 1px solid #C9D8AD& ...

  6. [笔记-图论]Floyd

    用于可带负权的多源最短路 时间复杂度O(n^3) 注意一定不要给Floyd一个带负环的图,不然就没有什么意义了(最短路不存在) 模板 // Floyd // to get minumum distan ...

  7. Configure Tomcat 7 to run Python CGI scripts in windows(Win7系统配置tomcat服务器,使用python进行cgi编程)

    Pre-installation requirements1. Java2. Python steps1. Download latest version of Tomcat (Tomcat 7) f ...

  8. 【习题 8-20 UVA-1620】Lazy Susan

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现,如果把连续4个数字进行一次翻转的话. 假设这连续的4个数字的逆序数为x; 那么翻转过后,逆序数就会变成6-x; (最多6个逆 ...

  9. HDU 3277 Marriage Match III

    Marriage Match III Time Limit: 4000ms Memory Limit: 32768KB This problem will be judged on HDU. Orig ...

  10. typedef 与 set_new_handler的几种写法

    可以用Command模式.函数对象来代替函数指针,获得以下的好处: 1. 可以封装数据 2. 可以通过虚拟成员获得函数的多态性 3. 可以处理类层次结果,将Command与Prototype模式相结合 ...