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) { // 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(); }