zoukankan      html  css  js  c++  java
  • interface

    interface Ishirou
        {       
            void Hunt();
        }
        interface Ishicao
        {
            void EatGrass();
        }
        abstract class Animal
        {
            public abstract void Eat();     
        }
        class Cat : Animal,Ishirou
        {
            public override void Eat() { Console.WriteLine("猫吃鱼"); }  
            public void Hunt()
            {
                Console.WriteLine("猫捉老鼠");           
            }
        }
        class Tiger : Animal, Ishirou
        {
            public override void Eat() { Console.WriteLine("老虎也吃鱼"); }
            public void Hunt()
            {
                Console.WriteLine("老虎捉羊");
            }
        }
        class Sheep : Animal, Ishicao
        {
            public override void Eat() { Console.WriteLine("羊吃草"); }  
            public void EatGrass()
            {          
            }
        }

        class Program
        {       
            static void Main(string[] args)
            {  
                List<Animal> animals = new List<Animal>();
                Animal a1 = new Cat();
                animals.Add(a1);
                Animal a2 = new Sheep();
                animals.Add(a2);
                Animal a3 = new Sheep();
                animals.Add(a3);
                Animal a4 = new Tiger();
                animals.Add(a4);          

                foreach (Animal a in animals)
                {
                    if (a is Ishirou)
                    {
                        Ishirou irou = (Ishirou)a;                    
                        irou.Hunt();
                    }
                }
                            
            }
        }
  • 相关阅读:
    nginx主配置文件详解
    微信网页第三方登录原理
    QQ第三方登陆流程详解
    php垃圾回收机制
    mysql索引
    MySQL性能优化的最佳20+条经验
    MYSQL explain详解
    mysql分区功能详细介绍,以及实例
    MySQL分表、分区
    Redis主从读写分离配置
  • 原文地址:https://www.cnblogs.com/imxh/p/2455636.html
Copyright © 2011-2022 走看看