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[] pets = new Pet[] { new Dog("Wangcai"), new Cat("Xiaoming"), new Dog("xiaoPeng") }; //也可以使用基类的引用,只能调用基类中的方法 //Pet dog2 = new Dog("xxx"); for (int i = 0; i < pets.Length; i++) { pets[i].Speak(); } Console.ReadKey(); Cat xiaoming = new Cat("xiaoming"); ICatchMouse icatchmouse = (ICatchMouse)xiaoming; IClimbTree iclimbtree = (IClimbTree)xiaoming; //通过对象调用 xiaoming.CatchMouse(); xiaoming.ClimbTree(); //通过接口调用 icatchmouse.CatchMouse(); iclimbtree.ClimbTree(); Console.ReadKey(); //调用静态方法 Dog.ShowNum(); Console.ReadKey(); //使用静态类扩展的方法 Dog dog = new Dog("ergouzi"); dog.HowToFeedDog(); Console.ReadKey(); }
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(); }