接口隔离原则

// 学习接口隔离原则
using System;

namespace console
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person(new Car());
            Person p1 = new Person(new Tank());
        }

    }
    interface ICanRun{
        void run();
    }
    interface ICanFire{
        void fire();
    }
    interface ITank{
        void run();
        void fire();
    }

    class Person{
        public Person(ICanRun icanrun){
            icanrun.run();
        }
    }
    class Car:ICanRun{
        public void run(){
            Console.WriteLine("开车了");
        }
    }
    class Tank :ICanRun,ICanFire{
        public void run(){
            Console.WriteLine("坦克开车了");
        }
        public void fire(){
            Console.WriteLine("开炮了");
        }
    }

}

 

posted @ 2019-08-28 22:39  liliyou  阅读(81)  评论(0编辑  收藏  举报