- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Collections;
- namespace day01
- {
- class Class25
- {
- static void Main(string[] args)
- {
- Student list = new Student();
- Student stu1 = new Student("zhangsan", 23);
- Student stu2 = new Student("lisi", 24);
- Student stu3 = new Student("wangwu", 25);
- list.Add(stu1);
- list.Add(stu2);
- list.Add(stu3);
- foreach(Student temp in list)
- {
- Console.WriteLine(temp.ToString());
- }
- Console.WriteLine();
- Console.WriteLine(list[2].ToString());
- Console.ReadKey();
- }
- }
- // 自定义集合类和实现索引符
- public class Student : CollectionBase
- {
- private string name;
- private int age;
- public string Name { get; set; }
- public int Age { get; set;}
- // 这两个构造器实现的功能完全不一样 但却放在同一个类中 感觉有问题
- // 无参构造器用于创建集合对象
- public Student()
- {
- }
- // 创建 Student 对象
- public Student(string name,int age)
- {
- Name = name;
- Age = age;
- }
- public override string ToString()
- {
- return "name:" + Name + "age:" + Age;
- }
- public void Add(Student stu)
- {
- List.Add(stu);
- }
- public void Remove(Student stu)
- {
- List.Remove(stu);
- }
- // 创建索引符
- public Student this[int index]
- {
- get
- {
- return (Student)List[index];
- }
- set
- {
- List[index] = value;
- }
- }
- }
- }
代码二
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Collections;
- namespace day01
- {
- class Class26
- {
- static void Main(string[] args)
- {
- // 这行代码该如何理解 调用的构造函数该怎么理解?
- Cat catList = new Cat();
- Cat cat1 = new Cat("zhangsan");
- Cat cat2 = new Cat("lisi");
- Cat cat3 = new Cat("wangwu");
- catList.Add(cat1);
- catList.Add(cat2);
- catList.Add(cat3);
- foreach(Cat temp in catList)
- {
- Console.WriteLine(temp.ToString());
- temp.Run();
- temp.Sleep();
- }
- Console.WriteLine();
- Console.WriteLine(catList[2].ToString());
- catList[2].Run();
- catList[2].Sleep();
- Console.ReadKey();
- }
- }
- public abstract class Animal : CollectionBase
- {
- protected string name;
- public string Name { get; set; }
- public Animal()
- {
- Name = "default";
- }
- public Animal(string name)
- {
- Name = name;
- }
- public abstract void Run();
- public abstract void Sleep();
- public override string ToString()
- {
- return "name:" + Name;
- }
- public void Add(Animal animal)
- {
- List.Add(animal);
- }
- public void Remove(Animal animal)
- {
- List.Remove(animal);
- }
- // 注意这行代码
- // 通过索引返回的对象类型为 Animal 类型 而不是 Object 类型 不需要再强制类型转换
- public Animal this[int index]
- {
- get
- {
- return (Animal)List[index];
- }
- set
- {
- List[index] = value;
- }
- }
- }
- public class Cat : Animal
- {
- // 这个构造器该怎么理解?
- public Cat()
- {
- }
- public Cat(string name):base(name)
- {
- }
- public override void Run()
- {
- Console.WriteLine("animal name: {0} is running",Name);
- }
- public override void Sleep()
- {
- Console.WriteLine("animal name: {0} is sleeping", Name);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3015795.html