C# 接口

1. 什么是接口

  • 在软件领域,实现标准化的一种方法是制定统一的接口(Interface)。
  • 比如许多银行会编写自己的账户系统,但他们都赞同使用相同的接口,以便在不同银行之间进行转帐业务。
  • 接口只规定系统具有哪些成员以及每个成员的功能,具体如何实现由各个公司自己设计,也就是说接口为大家制定了一个规范,然后各公司具体实现这个规范。

2. 银行账户类——实现接口

现在我们编写一个实现了 IBankAccount 接口的银行账户类,不同的银行可以有不同的实现方法,但必须实现 IBankAccount 接口中的所有成员

using System;

namespace BankAccount
{
    class Program
    {
        static void Main(string[] args)
        {
            //我们用 IBankAccount 接口的引用符 myAccount 指向了SaverAccount 类的对象。
            //实际上,如果有多个类派生于 IBankAccount 接口,接口引用符myAccount 可以指向所有派生类的对象,这体现了接口的多态性。
            IBankAccount myAccount = new SaverAccount();
            myAccount.PayIn(1000);
            myAccount.Withdraw(200);
            Console.WriteLine("余额:{0}", myAccount.Balance);
        }
    }

    #region 定义接口
    //接口用关键字 interface 定义,接口的名称习惯上以字母 I 开头。
    //接口中只能包含成员的声明,不能有任何实现代码。
    //接口的成员总是公有的,不需要也不能添加public等修饰符,也不能声明为虚方法或静态方法。
    interface IBankAccount
    {
        //方法:存款
        void PayIn(decimal amount);
        //方法:取款
        bool Withdraw(decimal amount);
        //方法:账户余额
        decimal Balance { get; }
    }
    #endregion

    #region 实现接口的类
    //SaverAccount类中实现了IBankAccount接口的所有成员
    //注意,实现接口的类的相应成员必须添加 public 修饰,并且可以声明为虚方法。
    class SaverAccount : IBankAccount
    {
        //私有变量:余额
        private decimal balance;
        //方法:存款
        public void PayIn(decimal amount)
        {
            balance += amount;
        }
        //方法:取款
        public bool Withdraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance -= amount;
                return true;
            }
            else
            {
                Console.WriteLine("余额不足,取款失败。");
                return false;
            }
        }
        //方法:余额
        public decimal Balance
        {
            get
            {
                return balance;
            }
        }
    }
    #endregion
}

3. 可转账的银行账户——接口的继承

using System;

namespace BankAccount
{
    class Program
    {
        static void Main(string[] args)
        {
            //我们用 IBankAccount 接口的引用符 myAccount 指向了SaverAccount 类的对象。
            //实际上,如果有多个类派生于 IBankAccount 接口,接口引用符myAccount 可以指向所有派生类的对象,这体现了接口的多态性。
            IBankAccount myAccount = new SaverAccount();
            myAccount.PayIn(1000);
            myAccount.Withdraw(200);
            Console.WriteLine("余额:{0}", myAccount.Balance);

            IBankAccount myAccount1 = new SaverAccount();
            ITransferBankAccount yourAccount1 = new TransferAccount();
            myAccount1.PayIn(10000);
            yourAccount1.PayIn(30000);
            //你向我转 5000 元
            yourAccount1.TransferTo(myAccount, 5000); 
            Console.WriteLine("我的余额:{0}", myAccount1.Balance);
            Console.WriteLine("你的余额:{0}", yourAccount1.Balance);
        }
    }

    #region 定义接口1
    //接口用关键字 interface 定义,接口的名称习惯上以字母 I 开头。
    //接口中只能包含成员的声明,不能有任何实现代码。
    //接口的成员总是公有的,不需要也不能添加public等修饰符,也不能声明为虚方法或静态方法。
    interface IBankAccount
    {
        //方法:存款
        void PayIn(decimal amount);
        //方法:取款
        bool Withdraw(decimal amount);
        //方法:账户余额
        decimal Balance { get; }
    }
    #endregion

    #region 定义接口2 (继承于接口1)
    //ITransferBankAccount 接口继承了 IBankAccount 接口的所有成员,并且定义了一个新成员方法 TransferTo()。    
    interface ITransferBankAccount : IBankAccount
    {
        //方法:转帐
        //注意 TransferTo()方法的第一个参数,其类型为 IBankAccount,
        //它巧妙地运用了接口的多态性,不管目标账户是什么类,只要这个类继承于 IBankAccount接口即可。
        bool TransferTo(IBankAccount destination, decimal amount);
    }
    #endregion


    #region 实现接口1的类
    //SaverAccount类中实现了IBankAccount接口的所有成员
    //注意,实现接口的类的相应成员必须添加 public 修饰,并且可以声明为虚方法。
    class SaverAccount : IBankAccount
    {
        //私有变量:余额
        private decimal balance;

        //方法:存款
        public void PayIn(decimal amount)
        {
            balance += amount;
        }
        //方法:取款
        public bool Withdraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance -= amount;
                return true;
            }
            else
            {
                Console.WriteLine("余额不足,取款失败。");
                return false;
            }
        }
        //方法:余额
        public decimal Balance
        {
            get
            {
                return balance;
            }
        }
    }
    #endregion

    #region 实现接口2的类
    class TransferAccount : ITransferBankAccount
    {
        //私有变量:余额
        private decimal balance;
        //方法:存款
        public void PayIn(decimal amount)
        {
            balance += amount;
        }
        //方法:取款
        public bool Withdraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance -= amount;
                return true;
            }
            else
            {
                Console.WriteLine("余额不足,取款失败。");
                return false;
            }
        }
        //属性:账户余额
        public decimal Balance
        {
            get
            {
                return balance;
            }
        }
        //方法:转账
        public bool TransferTo(IBankAccount destination, decimal amount)
        {
            bool result = Withdraw(amount);
            if (result == true)
            {
                destination.PayIn(amount);
            }
            return result;
        }
    }
    #endregion
}

在这里插入图片描述

4. 接口的多继承和显示实现

  • 在 C#中,类与类之间是单继承的,即一个类只能继承一个基类。但类与接口之间是多继承的,一个类可以实现多个接口。
using System;

namespace Breath
{
    class Program
    {
        static void Main(string[] args)
        {
            IAnimal life1 = new Life();
            IPlant life2 = new Life();
            life1.Breathe();
            life2.Breathe();
    }
    
    interface IAnimal
    {
        void Breathe();
    }
    public interface IPlant
    {
        void Breathe();
    }
    //该类继承了两个接口
    class Life : IAnimal, IPlant
    {
        void IAnimal.Breathe()
        {
            Console.WriteLine("我实现了接口IAnimal的Breathe方法。");
        }
        void IPlant.Breathe()
        {
            Console.WriteLine("我实现了接口IPlant的Breathe方法。");
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值