static void Main(string[] args) { Console.WriteLine("===============调用派生类方法=============="); Dog dog = new Dog(); //实例化对象 *调用派生类方法* dog._name = "Dog"; dog.Age = 12; dog.printfName(); dog.Speaking(); dog.running(); Console.WriteLine(); Console.WriteLine("===============采用多态的方法=============="); Pet dogs = new Dog(); //实例化对象 *采用多态的方法* dogs._name = "Dogs"; dogs.Age = 12; dogs.printfName(); //这时候对象装换为Pet对象后,这里new写的就不起作用了(接下一行) dogs.Speaking(); //输出的就会使基类的hello,而不是您好,这就是多态 dogs.running(); Console.WriteLine(); Console.WriteLine("===============利用数组批量读入=============="); Pet[] Pets = new Pet[] { new Dog1("优化了的TOM"), new Cat1("优化了的CAT") };//这里利用数组更加简便,在原函数中有该构造函数的存在 //注意这里数组的声明格式 int[] 名字 =new int[],比如这里是pet类创建数组,创建的数组叫做Pets, //等号后面 Pet[]{ 这里面是声明的个数组类型} for (int i = 0; i < Pets.Length; i++) //利用数字遍历每一个方法 { Pets[i].Speaking(); Pets[i].printfName(); } Console.WriteLine(); Console.WriteLine("===============派生类用基类构造函数的实例=============="); Dog2 dog2 = new Dog2("eminem"); //Dgg2类型是一个用了基类Pet1基类中构造函数的实例 dog2.printfName(); dog2.Speaking(); Console.WriteLine(); Console.WriteLine("===============抽象函数派生类实现后实例化=============="); Dog3 dog3 = new Dog3(); //抽象函数派生类实现后实例化 dog3.Speaking(); Console.WriteLine(); TaiDi dog4 = new TaiDi("豆豆"); dog4.printfName(); dog2.Speaking(); JinMao dog5 = new JinMao("金毛"); dog5.printfName(); dog5.catchThing(); //dog5.climbtree(); 这样会出错因为显式后,就不能用类实例实现方法了,应该用接口实例,如下 Iclimb cb = (Iclimb)dog5; //因为接口也是引用类型,但不能直接实例,通过强制转化显式实现的类的实例,来实例一个对象 cb.climbtree(); //通过对象来调用被写了的接口方法 Pet3 dog6 = new Pet3("ren"); dog6.printfName(); Pet3 dog7 = new Pet3("tian"); dog7.printfName(); dog7.e(); //由于上面用了静态类加静态方法通过this Pet3的方式加入了e()这一个方法,所以这里可以直接用 Console.WriteLine("===============显示隐式转换+重载操作符=============="); /*Dog2类型定义在102行,Cat2类型在其下面*/ Dog2 dog8 = new Dog2("互相转换的小动物"); //先定义一个名字叫“互相转换的小动物”的Dog2类型的动物 dog8.Speaking(); Cat2 cat2 = dog8; //将dog8【隐式转换】,转换成(cat2类型)赋值给cat2 cat2.Speaking(); Dog2 dog9 = (Dog2)cat2; //在这里将cat2【显式转换】,强制转换成(dog2类型)赋值给dog9 dog9.Speaking(); dog9++; //这里使用了一下重载操作符,将opera中的d1从1加到了3 dog9.num(); Console.WriteLine(); var dogCage = new Cage <Dog2>(1); //这里创建了一个cage泛型的实例化 dogCage.Putin(new Dog2("笼子动物1")); //将dog2类型的实例传入到dogcage中 dogCage.Putin(new Dog2("笼子动物2")); var dog10 = dogCage.Takeout(); //这里将该方法返回值赋值给了dog10 dog10.printfName(); //因为dog10得到了泛型中的返回值,所以其类型也是Dog2的,同样也拥有Dog2类中的方法 dog10.happy <int>(2323); //这里用了一个泛型方法 dog10.happy <huamn>(new huamn()); //同样可以这样传进来一个类 dogCage.Ishappy <Dog2>(new Dog2("泛型的小动物")); //这里是运用了泛型约束方法的实例化,所以需要满足约束,即必须是Dog2类型的参数传入 Idog dog11 = new Idog("泛型接口狗狗"); dog11.printfName(); dog11.Act(new SitCmd()); Console.WriteLine("===============下面是相关集合=============="); Console.WriteLine(); Console.WriteLine("===============【1】list<T>类=============="); List <Dog1> lists = new List <Dog1>(); //声明一个list的泛型类 lists.Add(new Dog1("list1")); //这里用了Add()方法 lists.Add(new Dog1("list2")); for (int i = 0; i < lists.Count; i++) //这里用了count属性 { lists[i].printfName(); } lists.Remove(lists[1]); Console.WriteLine("-------------下面是Remove掉一个后的--------------"); for (int i = 0; i < lists.Count; i++) { lists[i].printfName(); } Console.WriteLine(); Console.WriteLine("===============【2】Stack<T>类=============="); Stack <Dog1> stacks = new Stack <Dog1>(); stacks.Push(new Dog1("stack1")); stacks.Push(new Dog1("stack2")); Console.WriteLine("-------------peak查看栈顶但是不弹出--------------"); var dog12 = stacks.Peek(); dog12.printfName(); Console.WriteLine("-------------下面将栈顶的弹出--------------"); var dog13 = stacks.Pop(); dog13.printfName(); Console.WriteLine("-------------peak再次查看栈顶但是不弹出--------------"); dog12 = stacks.Peek(); dog12.printfName(); Console.WriteLine("===============【3】Queue<T>类=============="); Queue <Dog1> queues = new Queue <Dog1>(); queues.Enqueue(new Dog1("queue1")); queues.Enqueue(new Dog1("queue2")); foreach (Dog1 item in queues) //通过foreach遍历集合中的每一项 { item.printfName(); } Console.WriteLine("-------------插入两个弹出从先插入的弹出--------------"); var dog14 = queues.Dequeue(); dog14.printfName(); Console.WriteLine("===============【3】dictionary<T>类=============="); Dictionary <string, Dog1> dics = new Dictionary <string, Dog1>(); dics.Add("labuladuo", new Dog1("doc1")); dics.Add("guibin", new Dog1("doc2")); foreach (Dog1 item in dics.Values) //从valus中遍历各项 { item.printfName(); } Console.WriteLine("===============下面是委托的使用=============="); //委托声明不能放在mian里面,这里放在了mian的上面(也可以放在某一个类里面) Dog1 dog15 = new Dog1("委托的狗狗"); myDelegate mdl = dog15.Speaking; //注意这里只是赋值方法的引用,没有括号的! //myDelegate也是一种类型,所以要先实例化再使用 mdl(); Console.WriteLine(); Cat2 cat3 = new Cat2("委托的猫猫"); mdl += cat3.Speaking; //这里通过+=运算符将一个实例中再加入一个方法 mdl(); //这时候一次调用就可以调用两个方法 mdl += () => { Console.WriteLine("Lambda表达式在这里实现"); }; //在delegate实例的对象中加入一个Lambda表达式 mdl(); myDelegate2 mdl2 = (name) => { Console.WriteLine(name + "传入参数的狗狗在这里实现"); }; //mdl2是一个可以传入参数的委托类型 mdl2("Lambda"); //在上方用lambda表达式的方法对他进行了扩展 Console.ReadKey(); //Keep the console open in debug mode. }
public static void e(this Pet3 d) //静态方法,第一个参数用 this 类名 对象名 的方式来扩展Pet3类的方法 { Console.WriteLine("这只狗狗逃跑了!"); }