二刷:接口

1.接口

1.1抽象类与接口

image.png

1.2抽象类

  1. 抽象类中可以有实体方法也可以没有
  2. 一个抽象类继承另外一个抽象类,那么此时,子抽象类可以不用强制重写父抽象类的方法;
    abstract class VehicleBase
    {
        abstract public void Stop();
        abstract public void Run();
    }

    abstract class Vehicle: VehicleBase
    {
        abstract public void Fill();
    }

这类Vehicle继承了VehicleBase,但是没有重写里面的任何一个方法,且自己有重新定义了一个新的抽象方法

  1. 使用多态实例化的类(隐式声明),不可以调用父类没有的自己的实体方法;如果想调用,需要显示声明


    image.png
namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //多态(隐式声明)
            Vehicle v = new Car();
            v.Run();
            Console.ReadKey();
        }
    }
    abstract public class Vehicle
    {
        public virtual void Run()
        {
            Console.WriteLine("Vihicle is running....");
        }
        public void Cargo()
        {
            Console.WriteLine("Take Cargos");
        }
    }
    public class Car : Vehicle
    {
        public override void Run()
        {
            Console.WriteLine("Car is running....");
        }
        public void Music()
        {
            Console.WriteLine("play music...");
        }
    }
}
    1. 如果一个类,他会继承所有父级抽象类的抽象方法
namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
        
            //多态(隐式声明)
            Vehicle v = new Car();
            v.Run();
            Console.ReadKey();
        }
    }

    abstract public class BaseVehichle
    {
        abstract public void Run();
        abstract public void Stop();
    }
    abstract public class Vehicle:BaseVehichle
    {
        abstract public void Fill();
        public void Cargo()
        {
            Console.WriteLine("Take Cargos");
        }
    }

    public class Car : Vehicle
    {
        public override void Fill()
        {
            throw new NotImplementedException();
        }
        public void Music()
        {
            Console.WriteLine("play music...");
        }
        public override void Run()
        {
            throw new NotImplementedException();
        }
        public override void Stop()
        {
            throw new NotImplementedException();
        }
    }
}

1.3开放/关闭原则

image.png

这种写法违反了开闭原则;改进这种方法,直接使用虚方法

2.接口

2.1抽象类和接口的区别

  • 接口的方法必须是public,而抽象方法只要不是private就行
    不是用接口
namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var engine = new Engine();
            var car = new Car(engine);
            car.Run(3);
            Console.WriteLine(car.Speed);
            Console.ReadKey();
        }
    }

    class Engine
    {
        public int RPM { get; private set; }
        public void Work(int gas)
        {
            this.RPM = 1000 * gas;
        }
    }
    class Car
    {
        //耦合Engin类
        private Engine _engin;
        public Car(Engine engin)
        {
            _engin = engin;
        }
        public int Speed { get; private set; }
        public void Run(int gas)
        {
            _engin.Work(gas);
            this.Speed = _engin.RPM / 100;
        }
    }
}

注意:这样写的坏处就是,耦合严重,engine出了问题,很难发现哪里的错误;其次,就是engine出错,写car的程序员就只能等待engine修改好才可以进行工作

2.2使用接口解决紧密耦合

namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var user = new PhoneUser(new NokiaPhone());
            user.UserPhone();
            Console.ReadKey();
        }
    }
    class PhoneUser
    {
        private IPhone _phone;
        public PhoneUser(IPhone phone )
        {
            this._phone = phone;
        }
        public void UserPhone()
        {
            this._phone.Dail();
            this._phone.PickUp();
            this._phone.Send();
        }
    }
    interface IPhone
    {
        void Dail();
        void PickUp();
        void Send();
    }
    class NokiaPhone : IPhone
    {
        public void Dail()
        {
            Console.WriteLine("Nokia Dailing...");
        }
        public void PickUp()
        {
            Console.WriteLine("Nokia Hello...");
        }
        public void Send()
        {
            Console.WriteLine("Nokia Sending...");
        }
    }
    class SonyPhone : IPhone
    {
        public void Dail()
        {
            Console.WriteLine("Sony Dailing...");
        }
        public void PickUp()
        {
            Console.WriteLine("Sony Hello...");
        }
        public void Send()
        {
            Console.WriteLine("Sony Sending...");
        }
    }
}

这样用户可以通过传入的手机类,并调用里面的方法来实现,切换手机直接使用其功能,不用每次都进到类里面修改

2.3接口在单元测试中的应用

image.png

依赖关系:电扇依赖电源
如果有一天这个电扇不工作了,不知道是不是电扇坏了,假如为了测试哪里出了问题,调整了电源的电量,那么电源上的其他电器可能会因为调整了电源的电量发生错误,所以我们此时需要改进方法;

namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var fan = new DeskFan(new PowerSupply());
            Console.WriteLine(fan.Work());
            Console.ReadLine();
        }
    }
    class PowerSupply
    {
        //固定输出电量100
        //当发生错误电量变成300
        public int GetPower() { return 300; }
    }
    class DeskFan
    {
        private PowerSupply _powerSupply;
        public DeskFan(PowerSupply powerSupply)
        {
            this._powerSupply = powerSupply;
        }
        public string Work()
        {
            int power = _powerSupply.GetPower();
            if (power<=0)
            {
                return "won't work";

            }
            else if (power<100)
            {
                return "slow";
            }
            else if (power <200)
            {
                return "work fine";
            }
            else
            {
                return "Broken";
            }
        }
    }
}
  • 改进之后,只需要给电风扇传递不同的电源这样子就不需要改正式的电源了
namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var fan = new DeskFan(new PowerSupply());
            Console.WriteLine(fan.Work());
            Console.ReadLine();
        }
    }
    public interface IPowerSupply
    {
        int GetPower();
    }

    public class PowerSupply:IPowerSupply
    {
        //固定输出电量100
        //当发生错误电量变成300
        public int GetPower() { return 100; }
    }
    public class DeskFan
    {
        private IPowerSupply _powerSupply;
        public DeskFan(PowerSupply powerSupply)
        {
            this._powerSupply = powerSupply;
        }
        public string Work()
        {
            int power = _powerSupply.GetPower();
            if (power<=0)
            {
                return "won't work";

            }
            else if (power<100)
            {
                return "slow";
            }
            else if (power <200)
            {
                return "work fine";
            }
            else
            {
                return "Broken";
            }
        }
    }
}

2.4 接口隔离

违反接口隔离原则的设计1
namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var driver1 = new Driver(new Car());
            driver1.drive();
            Console.ReadKey();
        }
    }

    class Driver
    {
        private ITank _tank;
        private IVehichle _vehicle;
        //只可以开车不能开坦克
        public Driver(IVehichle vehichle)
        {
            this._vehicle = vehichle;
        }
        public void drive()
        {
            this._vehicle.Run();
        }

    }

    public interface IVehichle
    {
        void Run();
    }

    public class Car : IVehichle
    {
        public void Run()
        {
            Console.WriteLine("Car is running...");
        }
    }
    public class RaceCar : IVehichle
    {
        public void Run()
        {
            Console.WriteLine("RaceCar is running...");
        }
    }

    interface ITank
    {
        void Fire();
        void Run();
    }

    class LightTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Boom.....");
        }

        public void Run()
        {
            Console.WriteLine("LightTank is running...");
        }
    }

    class MediumTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Big Boom.....");
        }

        public void Run()
        {
            Console.WriteLine("MediumTank is running...");
        }
    }

    class HeavyTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Biggggggggggggggg  Boom.....");
        }

        public void Run()
        {
            Console.WriteLine("HeavyTank is running...");
        }
    }
}

这种设计会导致,驾驶员只能开车或者开坦克,其次就是,ITank接口里面,run并非只是Tank独有的,而是是有汽车都应该有的功能;

  • 改进:将杂糅的接口分离,使用接口继承来获得接口功能;
namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var driver1 = new Driver(new HeavyTank());
            var driver2= new Driver(new RaceCar());
            driver1.drive();
            driver2.drive();
            Console.ReadKey();
        }
    }

    class Driver
    {
        private IVehichle _vehicle;
        //只可以开车不能开坦克
        public Driver(IVehichle vehichle)
        {
            this._vehicle = vehichle;
        }
        public void drive()
        {
            this._vehicle.Run();
        }
    }
    interface IWeapon
    {
        void Fire();
    }
    
    public interface IVehichle
    {
        void Run();
    }

    public class Car : IVehichle
    {
        public void Run()
        {
            Console.WriteLine("Car is running...");
        }
    }
    public class RaceCar : IVehichle
    {
        public void Run()
        {
            Console.WriteLine("RaceCar is running...");
        }
    }
    interface ITank:IVehichle,IWeapon
    {
        void Fire();
        void Run();
    }
    class LightTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Boom.....");
        }

        public void Run()
        {
            Console.WriteLine("LightTank is running...");
        }
    }
    class MediumTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Big Boom.....");
        }

        public void Run()
        {
            Console.WriteLine("MediumTank is running...");
        }
    }
    class HeavyTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Biggggggggggggggg  Boom.....");
        }

        public void Run()
        {
            Console.WriteLine("HeavyTank is running...");
        }
    }
}

说明:改进后的代码实现的功能:实例化Driver之后,不管是传Tank还是Car,他都可以实现Run的功能,而由于普通的Driver并不是特殊人员,所以他只拥有driver的能力,这样通过接口实现了功能上的权限问题

接口顯示實現
namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var wk1 = new WarmKiller();//没Kill方法
            wk1.Love();
            IKiller wk2 = wk1;//显示实现,有Kill,无Love
            wk2.Kill();
            IKiller wk3 = new WarmKiller();//和上面一样
            wk3.Kill();
            //如果想调用Love那么,可以使用类型转换
            var wk4 = wk3 as IGentleman; //方法一
            wk4.Love();
            var wk5 = (IGentleman)wk3; //方法一
            wk5.Love();
            Console.ReadKey();
        }
    }
    interface IKiller
    {
        void Kill();
    }
    interface IGentleman
    {
        void Love();
    }
    class WarmKiller : IGentleman,IKiller
    {
        //普通实现
        public void Love()
        {
            Console.WriteLine("loving.....");
        }
        //显示实现:必须使用IKiller创建实例才会有kill方法
        void IKiller.Kill()
        {
            Console.WriteLine("killing....");
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容