static void Main(string[] args) { Pet[] pets = new Pet[] { new Dog("Jack"), new Cat("Tom"), new Dog("Cherry") }; for (int i = 0; i < pets.Length; i++) { pets[i].Speak(); } Cat c = new Cat("Tom2"); IClimbTree climb = (IClimbTree)c; c.ClimbTree(); climb.ClimbTree(); ICatchMice catchM = (ICatchMice)c; c.CatchMice(); catchM.CatchMice(); Dog.ShowNum(); //Pet dog = new Dog(); //dog.Name ="Jack"; //dog.PrintName(); //dog.Speak(); //Pet cat = new Cat(); //cat.Name = "Tom"; //cat.PrintName(); //cat.Speak(); }
static void Main(string[] args) { Cat c = new Cat("Tom2"); IClimbTree climb = (IClimbTree)c; c.ClimbTree(); climb.ClimbTree(); ICatchMice catchM = (ICatchMice)c; c.CatchMice(); catchM.CatchMice(); }
static void Main(string[] args) { // Pet dog = new Dog(); //dog._name = "Jack"; // dog.PrintName(); // Pet cat = new Cat(); //基类的引用 // cat._name = "Tom"; // cat.PrintName(); // dog.Speak(); // cat.Speak(); Pet[] pets = new Pet[] { new Dog("Jack"), new Cat("Tom"), new Dog("jack2") }; for (int i = 0; i < pets.Length; i++) { pets[i].PrintName(); pets[i].Speak(); } Cat c = new Cat("Tom2"); IClimbTree climb = (IClimbTree)c; c.ClimbTree(); climb.ClimbTree(); ICatchMice catchM = (ICatchMice)c; c.CatchMice(); catchM.CatchMice(); Dog.ShowNum(); Dog dog = new Dog("jack3"); dog.HowToFeedDog(); { int i = 3; object oi = i; //装箱 Console.WriteLine("i=" + i + "oi=" + oi.ToString()); oi = 10; //存储在堆中 i = 7; //存储在栈中 Console.WriteLine("i=" + i + "oi=" + oi.ToString()); int j = (int)oi; //拆箱 Console.WriteLine("j=" + j); } Console.Read(); }
static void Main(string[] args) { /* 没有添加构造方法之前 * * * // 初始化dog类 * Dog aDog = new Dog(); * aDog.Name = "旺财"; * aDog.PrintName(); * * //隐藏方法测试 * Cat aCat = new Cat(); * aCat.Name = "Alis"; * aCat.PrintName();// 打印子类cat中的PrintName方法 * * Pet aCat01 = new Cat(); * aCat01.Name = "Alis"; * aCat01.PrintName();// 打印父类pet中的PrintName方法 * //Tip:狗和猫都继承Pet类,如果没有使用多态,子类初始化的时候引用是父类,那就会调用父类的方法 * * * //虚方法和多态测试-----目的:子类初始化的时候引用父类,但是调用的还是子类自己的方法 * Pet aDog01 = new Dog(); * aDog01.Speak("狗说你好!");// 打印子类Dog中的Speak方法 * * Pet aCat02 = new Cat(); * aCat02.Speak("猫咪说你好!");// 打印子类Cat中的Speak方法 */ /* * 添加构造方法之后,可以直接一个数组来做上面的事 */ Pet[] pets = new Pet[] { new Dog("Jick"), new Cat("Tom") }; for (int i = 0; i < pets.Length; i++) { pets[i].Speak("你好"); /* 打印结果 * 父类中---狗类---说话的方法---你好 * 父类中---猫类---说话的方法---你好 */ pets[i].PrintName(); // 没有重写PrintName方法,所以打印的是父类中的PrintName方法 } Cat cat01 = new Cat("Tom02"); ICatchMice catchMice = (ICatchMice)cat01;// 强制转换 cat01.CatchMice(); catchMice.CatchMice(); // 调用静态函数 - 直接用类名调用 Dog.ShowDogNum(); // 调用静态类 Dog类本身没有这个方法,是使用静态类来添加的 Dog dog = new Dog("huahua"); dog.HowToFeed(); Dog dog02 = new Dog("wangcai"); dog02.Speak("wangwang"); // 开始转换 Cat cat02 = dog02; cat02.PrintName(); // 打印出名字还是叫wangcai,只是现在是一只猫 Dog dog03 = (Dog)cat02; // 显示转换 ,要添加转换类型 dog03.PrintName(); // 测试重载运算符 { Pet[] pets01 = new Pet[] { new Dog("Jick012"), new Cat("Tom023") }; for (int i = 0; i < pets01.Length; i++) { pets01[i]++; pets01[i].ShowAge(); } } // 泛型测试 var dogCage = new Cage <Dog>(1); dogCage.Putin(new Dog("A")); dogCage.Putin(new Dog("B")); var dogTemp = dogCage.Takeout(); dogTemp.PrintName(); // 调用泛型方法 var dog04 = new Dog("C"); dog04.IsHappy <int>(3); // 调用有约束的泛型方法 - 只能传入class var dog05 = new Dog("D"); dog05.IsHappy01 <Person>(new Person()); // 集合 List <Dog> list = new List <Dog>(); list.Add(new Dog("S")); list.Add(new Dog("SS")); list.Add(new Dog("SSS")); for (int i = 0; i < list.Count; i++) { list[i].PrintName(); } Dictionary <string, Dog> dic = new Dictionary <string, Dog>(); dic.Add("One", new Dog("B")); dic.Add("Two", new Dog("BB")); dic.Add("Three", new Dog("BBB")); dic["Two"].PrintName(); // 栈 - 先进后出 ---- 集合 Stack <Pet> stack = new Stack <Pet>(); stack.Push(new Dog("A")); stack.Push(new Cat("B")); stack.Peek().PrintName();// 打印最顶部的数据 stack.Pop(); stack.Peek().PrintName(); // 队列 先进先出 Queue <Pet> queue = new Queue <Pet>(); queue.Enqueue(new Dog("Aaaaaaaa")); queue.Enqueue(new Dog("Bbbbbbbb")); queue.Enqueue(new Dog("Cccccccc")); Pet pet = null; pet = queue.Dequeue(); // 出去 pet.PrintName(); pet = queue.Dequeue(); // 出去 pet.PrintName(); pet = queue.Dequeue(); // 出去 pet.PrintName(); // 委托 ActCute del = null; Cat cat00 = new Cat("dasda"); del = cat00.CeShiDeleage;// 注意 不需要打括号 // Lambda表达式 del += () => { Console.WriteLine("我是一个Lambda表达式定义的方法,我什么都没做"); }; del(); // 模拟客户 Client c1 = new Client(); Client c2 = new Client(); // 注册 //Dog.NewDog02 += c1.WantADog; //Dog.NewDog02 += c2.WantADog; Dog dog002 = new Dog("新来了一只狗"); Console.Read(); }