day08 c#接口的运用


    interface ISpeak
    {
        string Hello();
    }
    class Baby : ISpeak
    {
        public string Hello()
        {
            return "哇哇哇";
        }
    }

    interface IBattle
    {
        int Attack();
        int Move();
        void Jump();
    }
    interface IRest
    {
        void SitDown();
        void Sleep();
    }
    interface IPlayer : IBattle,IRest
    {
    }
    class Soldier : IPlayer
    {
        private string name;
        private int attack;
        private int speed;
        public Soldier(string name, int attack, int speed)
        {
            this.name = name;
            this.speed = speed;
            this.attack = attack;
        }

        public int Attack()
        {
            return attack;
        }

        public void Jump()
        {
            Console.WriteLine(name + "跳跃");
        }

        public int Move()
        {
            return speed;
        }
        public  string Name()
        {
            return name;
        }

        public void SitDown()
        {
            Console.WriteLine(name+"坐下了");
        }

        public void Sleep()
        {
            Console.WriteLine(name+"睡觉了");
        }
    }



    interface IUSB
    {
        string ReadData();
    }
    public abstract class MemoryDevice : IUSB
    {
        
        public string ReadData()
        {
            return "正在阅读数据";
        }
        public abstract void Description(MemoryType memoryType);
    }
    public enum MemoryType
    {
        SolidStateDisk,
        HardDisk,
        UDisk
    }

    class Computer : MemoryDevice
    {
        public override void Description(MemoryType memoryType)
        {
           
            switch (memoryType)
            {
                case MemoryType.SolidStateDisk:
                    Console.WriteLine("固态硬盘" + ReadData()); 
                    break;
                case MemoryType.HardDisk:
                    Console.WriteLine("机械硬盘" + ReadData());
                    break;
                case MemoryType.UDisk:
                    Console.WriteLine("U盘" + ReadData()); 
                    break;
            }
        }
    }
    class SingletonClass where T : class, new()
    {

        private static T singleton;

        public static T Singleton
        {
            get
            {
                if (singleton == null)
                {
                    singleton = new T();
                }
                return singleton;
            }
        }
    }

    class Person : SingletonClass
    {
        public void cw()
        {
            Console.WriteLine("666");
        }
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            Baby baby = new Baby();
            Console.WriteLine(baby.Hello());
            Console.WriteLine("-------------");

            Soldier soldier = new Soldier("zw", 1000, 300);
            Console.WriteLine(soldier.Name() + "的攻击力是" + soldier.Attack());
            Console.WriteLine(soldier.Name() + "的移速是" + soldier.Move());
            soldier.Jump();
            soldier.SitDown();
            soldier.Sleep();
            Console.WriteLine("-------------");

            Computer computer = new Computer();
            Console.WriteLine("插入的是什么?1-固态硬盘。2-机械硬盘。3-U盘");
            MemoryType memoryType=new MemoryType();
            memoryType=(MemoryType )int.Parse(Console.ReadLine())-1;
            computer.Description(memoryType);
    }

 

 

day08 c#接口的运用_第1张图片

你可能感兴趣的:(c#)