예제 #1
0
        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();
        }
예제 #2
0
파일: Program.cs 프로젝트: wula50/csharp
 public static void catchTask(ICatchMouse c)
 {
     c.Catch();
 }