zoukankan      html  css  js  c++  java
  • 集合数组使用的区别

    System.Collections 命名空间的一个类System.Collections.ArrayList实现了IList、Icollection、IEnumerable接口,但实现的方式比System.Array复杂。
    数组的大小是固定的(不能增加或者删除元素)
    ArrayLIst可以用于表示大小可变的项目列表;
    下面用代码演示此结果
    代码
    1 ///  
    2 /// 抽象类
    3 ///  
    4   public abstract class Animal
    5 {
    6 private string name;
    7 public string Name
    8 {
    9 get { return name; }
    10 set { name = value; }
    11 }
    12 public Animal()
    13 {
    14 Name = "the Animal with no name";
    15 }
    16 public Animal(string newName)
    17 {
    18 Name = newName;
    19 }
    20 public void Feed()
    21 {
    22 Console.WriteLine("{0} has been fed", name);
    23 }
    24 }
    代码
    1 ///  
    2 /// 派生类
    3 ///
    4   public class Cow : Animal
    5 {
    6 public void Milk()
    7 {
    8 Console.WriteLine("{0} has been milked",Name);
    9 }
    10 public Cow(string newName)
    11 : base(newName)
    12 {
    13 }
    14 }
    15 ///
    16 /// 派生类
    17 ///
    18   public class Chicken : Animal
    19 {
    20 public void LayEgg()
    21 {
    22 Console.WriteLine("{0} has laid an egg", Name);
    23 }
    24 public Chicken(string newName)
    25 : base(newName)
    26 {
    27 }
    28 }
     
     实现方式:
    代码
    static void Main(string[] args)
    {
    Console.WriteLine(
    "Create an Array type collection of Animal objects and use it:");
    Animal[] animalArray
    = new Animal[2];
    animalArray[
    0] = new Cow("Deirdre");
    animalArray[
    1] = new Chicken("Ken");
    foreach (Animal myAni in animalArray)
    {
    Console.WriteLine(
    "New {0} object added to Array collection, Name= {1}", myAni.ToString(), myAni.Name);
    }
    animalArray[
    0].Feed();
    ((Cow)animalArray[
    0]).Milk();
    animalArray[
    1].Feed();
    ((Chicken)animalArray[
    1]).LayEgg();

    Console.WriteLine(
    "Create an ArrayList type collection of Animal objects and use it:");
    ArrayList list
    = new ArrayList();
    list.Add(
    new Cow("HayLey"));
    list.Add(
    new Chicken("Roy"));
    foreach (Animal item in list)
    {
    Console.WriteLine(
    "New {0} object added to ArrayList collection ,Name = {1} ", item.ToString(), item.Name);
    }
    Console.WriteLine(
    "ArrayList collection contains {0} objects.", list.Count);
    ((Animal)list[
    0]).Feed();
    ((Cow)list[
    0]).Milk();
    ((Animal)list[
    1]).Feed();
    ((Chicken)list[
    1]).LayEgg();

    list.RemoveAt(
    0);
    ((Animal)list[
    0]).Feed();
    ((Chicken)list[
    0]).LayEgg();
    list.AddRange(animalArray);
    Cow c
    = new Cow("small cow");
    list.Add(c);
    c.Name
    = "Big cow";
    ((Cow)list[
    3]).Milk();
    }
    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    核心动画03-图标抖动.mp4
    核心动画02-CAKeyframeAnimation帧动画
    核心动画01-CABasicAnimation
    CALayer参考文献
    CALayer04-自定义图层
    CALayer03-隐式动画
    CALayer02-创建新图层
    iOS开发中的网络请求
    IT教育课程考评系统开发-06
    IT教育课程考评系统开发-05
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1907711.html
Copyright © 2011-2022 走看看