标签:running string for cat day 强制 val abstract obj
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 Class25 12 { 13 static void Main(string[] args) 14 { 15 Student list = new Student(); 16 Student stu1 = new Student("zhangsan", 23); 17 Student stu2 = new Student("lisi", 24); 18 Student stu3 = new Student("wangwu", 25); 19 20 list.Add(stu1); 21 list.Add(stu2); 22 list.Add(stu3); 23 24 25 foreach(Student temp in list) 26 { 27 Console.WriteLine(temp.ToString()); 28 } 29 30 Console.WriteLine(); 31 Console.WriteLine(list[2].ToString()); 32 Console.ReadKey(); 33 } 34 } 35 //自定义集合类和实现索引符 36 public class Student : CollectionBase 37 { 38 private string name; 39 private int age; 40 public string Name { get; set; } 41 public int Age { get; set;} 42 //这两个构造器实现的功能完全不一样 但却放在同一个类中 感觉有问题 43 //无参构造器用于创建集合对象 44 public Student() 45 { 46 47 } 48 //创建Student对象 49 public Student(string name,int age) 50 { 51 Name = name; 52 Age = age; 53 } 54 public override string ToString() 55 { 56 return "name: " + Name + " age: " + Age; 57 } 58 public void Add(Student stu) 59 { 60 List.Add(stu); 61 } 62 public void Remove(Student stu) 63 { 64 List.Remove(stu); 65 } 66 //创建索引符 67 public Student this[int index] 68 { 69 get 70 { 71 return (Student)List[index]; 72 } 73 set 74 { 75 List[index] = value; 76 } 77 } 78 } 79 }
代码二
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 Class26 12 { 13 static void Main(string[] args) 14 { 15 //这行代码该如何理解 调用的构造函数该怎么理解? 16 Cat catList = new Cat(); 17 Cat cat1 = new Cat("zhangsan"); 18 Cat cat2 = new Cat("lisi"); 19 Cat cat3 = new Cat("wangwu"); 20 catList.Add(cat1); 21 catList.Add(cat2); 22 catList.Add(cat3); 23 24 foreach(Cat temp in catList) 25 { 26 Console.WriteLine(temp.ToString()); 27 temp.Run(); 28 temp.Sleep(); 29 } 30 Console.WriteLine(); 31 32 Console.WriteLine(catList[2].ToString()); 33 catList[2].Run(); 34 catList[2].Sleep(); 35 36 Console.ReadKey(); 37 } 38 } 39 public abstract class Animal : CollectionBase 40 { 41 protected string name; 42 public string Name { get; set; } 43 public Animal() 44 { 45 Name = "default"; 46 } 47 public Animal(string name) 48 { 49 Name = name; 50 } 51 public abstract void Run(); 52 public abstract void Sleep(); 53 public override string ToString() 54 { 55 return "name: " + Name; 56 } 57 public void Add(Animal animal) 58 { 59 List.Add(animal); 60 } 61 public void Remove(Animal animal) 62 { 63 List.Remove(animal); 64 } 65 //注意这行代码 66 //通过索引返回的对象类型为Animal类型 而不是Object类型 不需要再强制类型转换 67 public Animal this[int index] 68 { 69 get 70 { 71 return (Animal)List[index]; 72 } 73 set 74 { 75 List[index] = value; 76 } 77 } 78 } 79 public class Cat : Animal 80 { 81 //这个构造器该怎么理解? 82 public Cat() 83 { 84 85 } 86 public Cat(string name):base(name) 87 { 88 89 } 90 public override void Run() 91 { 92 Console.WriteLine("animal name: {0} is running",Name); 93 } 94 95 public override void Sleep() 96 { 97 Console.WriteLine("animal name: {0} is sleeping", Name); 98 } 99 } 100 }
标签:running string for cat day 强制 val abstract obj
原文地址:https://www.cnblogs.com/littlelittleprince/p/10669813.html