• 集合数组使用的区别


    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();
    }
    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    mysql函数
    maven 配置自动本地/线上不同配置自动打包
    maven clean后 编译报错
    htmlunit填坑
    java正则表达式移除网页中注释代码
    spark 计算结果写入mysql 案例及常见问题解决
    pychrome激活
    hadoop集群常见问题解决
    hadoop+spark集群搭建
    C++:构造函数2——拷贝构造函数
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1907711.html
Copyright © 2020-2023  润新知