接口

来源:互联网 发布:冰川网络上市股价多少 编辑:程序博客网 时间:2024/04/28 02:41

using System;
namespace LearningCsharp
{
    interface ICarnivore
    {
        bool IsHungry { get;}
        Animal Hunt();
        void Eat(Animal victim);
    }
    public abstract class Animal
    {
        public abstract void Sleep();
    }
    public class Antelope : Animal
    {
        public override void Sleep() { }      
    }
    public class Lion : Animal, ICarnivore
    {
        public Lion()
        {
            hungry = true;
        }
        private bool hungry;
        public bool IsHungry
        {
            get { return hungry; }
        }
        public Animal Hunt()
        {
            return new Antelope();
        }
        public void Eat(Animal prey)
        {
            Console.WriteLine("Lion is no longer hungry");
        }
        public override void Sleep() { }

        public void JoinPride() { }
    }
    class Tester
    {
        static void Main(string[] args)
        {
            Lion aLion = new Lion();
            if (aLion.IsHungry)
            {
                Animal victim = aLion.Hunt();
                if (victim != null)
                {
                    aLion.Eat(victim);
                }
            }

            aLion.JoinPride();
            aLion.Sleep();
        }
    }
}

原创粉丝点击