惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
D
Docker
云风的 BLOG
云风的 BLOG
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
GbyAI
GbyAI
T
Tailwind CSS Blog
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
C
Check Point Blog
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
N
Netflix TechBlog - Medium
B
Blog RSS Feed
腾讯CDC
aimingoo的专栏
aimingoo的专栏

博客园 - 言午

Nopcommerce 二次开发2 Admin Nopcommerce 二次开发1 基础 Nopcommerce 二次开发2 WEB Nopcommerce 二次开发0 sqlce中不支持sp_rename修改表名 C#读取Excel遇到无法读取的解决方法 狼奔代码生成器 银行账户类 累 抽象类 抽象方法 Message 类的继承 多态(虚方法) 事件event 委托 代理 索引! 第五周作业 第四周作业 考试! 线程安全 二 线程安全 一
interface
言午 · 2012-04-18 · via 博客园 - 言午

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();
                }
            }
                        
        }
    }