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

推荐订阅源

爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - Franky
D
Docker
B
Blog RSS Feed
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
B
Blog
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News

博客园 - 言午

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