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();
                    }
                }
                            
            }
        }
  • 相关阅读:
    ES6特性
    使用mybatis插件拦截SQL
    前端下载文件的几种方式
    Electron-vue项目使用 Inno Setup 创建安装包
    Windows powershell 常用代码段
    Java8之Predicate, Consumer,Function基础使用
    Java8之Predicate接口使用
    使用Replica Set副本集方式搭建mongodb副本集群
    Typora的一些偏好设置
    使用Typora编写md文档并优雅地上传到博客园
  • 原文地址:https://www.cnblogs.com/imxh/p/2455636.html
Copyright © 2011-2022 走看看