[C#]关于接口Interface的场景假设-代码模拟-引喻类比等-C

前言

接口和类的作用很相似,但又有所不同。在面向对象程序设计高级语言中,类的概念比接口的概念出现的早。如何快速地分辨他们的语义差别,如何快速地建立起自己的抽象模型,如何快速地应用到自己的代码实践当中,是初入程序设计行业的人员急需关注的问题。同时,在此声明,本栏目中接口系列的文字论断皆为一家之言。C篇属于接口系列第3篇,欢迎抛转,共同进步。

场景假设

假设一个进入ICU房间的重病人,身上安装了一个带有插座(Socket)接口的设备,需要外部的的医疗护理设备的插头(Plug)相连,才可以维持生命特征。工作过程:插头和插座相连后,外部设备首先启动,然后再引起病人身上的设备的启动。

代码模拟

using System;

namespace InterfaceDemo
{
    public interface ISocket
    {   
        void StartWork(int eQuantity);
    }
    interface IPlug { 
        void  StartWorkNow();     
    }
    public class MaintainLifeEquipment : IPlug
    {
        private ISocket _iSocket;
        private int _eQuantity;
        public MaintainLifeEquipment(ISocket ISocket,int eQuantity)
        {
            _iSocket = ISocket;
            _eQuantity = eQuantity;
        }
        public void StartWorkNow()
        {
            _iSocket.StartWork(_eQuantity);
        }  
    }    

    public class Patient
    {
        prote

你可能感兴趣的:(代码哲学,c#)