static public void HowToFeedDog(this Dog dog) { Console.WriteLine("Play a video about how to feed a dog"); }
static public void HowToFeedDog(this Dog dog) { Console.WriteLine("play video about how to feed pet"); }
delegate void ActCute(); //委托 static void Main(string[] args) { /*基类的引用 派生类的对象包含基类部分和派生部分,所以 * 我们可以通过一个基类类型的引用指向派生类。 * //Pet dog = new Dog(); 通过指向派生类的基类引用,我们仅仅能访问派生类中的基类部分*/ /* Dog dog = new Dog(); * dog._name = "jack"; * dog.PrintName(); * //Pet dog = new Dog(); * Cat cat = new Cat(); * * cat._name = "tom"; * cat.PrintName(); */ /* Pet[] pets = new Pet[]{new Dog("Jack"),new Cat("tom") }; //基类的引用指向派生类 调用的是重写的方法 * for(int i=0; i < pets.Length; i++) * { * pets[i].Speak(); * } * Dog c = new Dog("tom2"); * ICatchMice Catch = (ICatchMice)c; * c.CatchMice(); * Catch.CatchMice(); //只能调用接口内的方法 * IClimTree Clim = (IClimTree)c; * Clim.ClimTree(); * Cat.ShowNum(); * Dog dog = new Dog("dd"); * 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("oi= " + j); * } */ /* Dog dog = new Dog("jack"); * dog.Speak(); * Cat cat = dog; //隐示转换 * cat.Speak(); * cat.PrintName(); * * Dog dog2 = (Dog)cat; //显示转换 * cat.Speak(); */ /* Pet[] pets = new Pet[]{new Dog("Jack"),new Cat("tom") }; * for(int i=0; i < pets.Length; i++) * { * pets[i]++; * pets[i].ShowAge(); * * } */ /* var catCage = new Cage<Cat>(1); * catCage.Putin(new Cat("A")); * catCage.Putin(new Cat("b")); * var cat = catCage.Takeout(); * cat.PrintName(); 范指类 */ var dog = new Dog("A"); dog.isHappy <Person>(new Person()); // dog.isHappy<int>(3); // dog.isHappy<Cat>(new Cat("d")); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>范型接口 /* Labrador dog = new Labrador("A"); * dog.Act(new SitDogCmd());*/ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>范型接口 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>集合 /* List<Dog> list = new List<Dog>(); //lis<>方法 * list.Add(new Dog("A")); * list.Add(new Dog("B")); * list.Add(new Dog("C")); * list.RemoveAt(1); * * for (int i= 0; i < list.Count; ++i) * { * list[i].PrintName(); * } */ /* Dictionary<string, Dog> dic = new Dictionary<string, Dog>(); //字典 * dic.Add("a", new Dog("A")); * dic.Add("a", new Dog("B")); * dic.Add("a", new Dog("C")); * dic["A"].PrintName(); * * Stack<Pet> stack = new Stack<Pet>(); //stack 栈 * 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("D")); * queue.Enqueue(new Dog("E")); * queue.Enqueue(new Cat("F")); * * Pet p = null; * p = queue.Dequeue(); * p.PrintName(); * p = queue.Dequeue(); * p.PrintName(); * p = queue.Dequeue(); * p.PrintName(); */ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>集合 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>委托 匿名函数lambda表达式 /* ActCute del = null; * Dog dog = new Dog("A"); * Cat cat = new Cat("B"); * del = dog.WagTail; * del+= cat.InnocentLook; * del += () => // lambda表达式 * { * Console.WriteLine("do nothing"); * }; * * del(); //委托调用 与函数相同 有参数在括号 调用出所有的函数方法 * */ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>委托 匿名函数lambda表达式 /* client c1 = new client(); * client c2 = new client(); * Cat.NewCat += c1.WantADog; //通过对象订阅 * Cat.NewCat += c2.WantADog; * Cat cat = new Cat("q"); */ }
static void Main(string[] args) { var dog = new Dog("A"); dog.IsHappy <Cat>(new Cat("Tom")); //dog.IsHappy<int>( 3); //var dogCage = new Cage<Dog>(1); //dogCage.Putin(new Dog("A")); //dogCage.Putin(new Dog("B")); //var dog = dogCage.TakeOut(); //dog.PrintName(); //Dog dog = new Dog("Jack"); //dog.Speak(); //Cat cat = dog; //cat.Speak(); //cat.PrintName(); //Dog dog2=(Dog)cat; //dog2.Speak(); //Pet[] pets = new Pet[] { new Dog("Jack"), new Cat("Tom"), new Dog("Cherry") }; //for (int i=0;i<pets.Length;++i) //{ // pets[i]++; // pets[i].ShowAge(); //} //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(); //Dog dog = new Dog("Tommy"); //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); //} //Pet dog = new Dog(); //dog.Name ="Jack"; //dog.PrintName(); //dog.Speak(); //Pet cat = new Cat(); //cat.Name = "Tom"; //cat.PrintName(); //cat.Speak(); }