C#继承接口的类和派生接口的实现

接口语法上与声明抽象类完全相同,但是不允许提供接口中任何成员的实现方式。一般情况只能包含方法,属性,索引器,和事件声明,不能实例化。


实现接口:接口表示成员的存在性,类负责确定这些方法的实现代码,如果缺少实现代码,编译器会产生错误。

接口可以引用任何实现该接口的类。


源代码如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace InterfaceDemo2
{


    #region 声明一个接口
    public interface IBankAccount
    {
        void PayIn(decimal amount);
        bool WithDraw(decimal amount);
        decimal Balance { get; }
    }
    #endregion


    #region 第1个类实现接口,即继承该接口
    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;
            }


            Console.WriteLine("WithDraw Faileed!");
            return false;
        }


       public  decimal Balance
        {
           get
            {
                return balance;
            }
        }


       public override string ToString()
       {
           return String.Format("Balance is :{0,6:C}", balance);
       }
    }
    #endregion


    #region 第2个类实现接口,即继承该接口
    public class SaverAccount2: IBankAccount
    {
        private decimal balance;
        public void PayIn(decimal amount)
        {
            balance += amount;
        }


        public bool WithDraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance -= amount;
                return true;
            }


            Console.WriteLine("WithDraw Faileed!");
            return false;
        }


        public decimal Balance
        {
            get
            {
                return balance;
            }
        }


        public override string ToString()
        {
            return String.Format("Balance is :{0,6:C}", balance);
        }
    }
    #endregion


    #region 派生的接口
    public interface ITransferBankAccount :IBankAccount
    {
        //除了IBankAccount中的成员和方法外,新增TransferTo方法
        bool TransferTo(IBankAccount destination, decimal amonut);
    }
    #endregion




    #region 继承派生类
    public class CurrentAccount:ITransferBankAccount
    {
        private decimal balance;
        public void PayIn(decimal amount)
        {
            balance += amount;
        }


        public bool WithDraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance -= amount;
                return true;
            }


            Console.WriteLine("WithDraw Faileed!");
            return false;
        }


        public decimal Balance
        {
            get
            {
                return balance;
            }
        }


        public override string ToString()
        {
            return String.Format("Balance is :{0,6:C}", balance);
        }


        //新增加TransferTo实现函数
        public bool TransferTo(IBankAccount destination,decimal amount)
        {
            bool result;
            result = WithDraw(amount);
            if (result )
            {
                destination.PayIn(amount);
            }
            return result;
        }
    }
    #endregion


    class Program
    {
        static void Main(string[] args)
        {
            IBankAccount Acconut1 = new SaverAccount(); //接口可以引用任何实现该接口的类
            Acconut1.PayIn(200);
            Acconut1.WithDraw(50);
           // Acconut1.WithDraw(500);
            Console.WriteLine("The Account1 Balance is {0}", Acconut1.Balance.ToString ());


            //实现第2个类的方法
            IBankAccount Account2 = new SaverAccount2();
            Account2.PayIn(1000);
            Account2.WithDraw(1200);
            Account2.PayIn(100);
            Console.WriteLine("The Account2 Balance is {0}", Account2.Balance.ToString ());


            //派生接口,类的实现
            ITransferBankAccount ACT = new CurrentAccount();
            ACT.PayIn(500);
            ACT.TransferTo(Acconut1, 200); //转到第一个账户上
            Console.WriteLine("The first Account Banlace is {0:6:C}", Acconut1.Balance.ToString());
            Console.WriteLine("The Current Account Banlace is {0:6:C}", ACT.Balance.ToString());






            Console.Read();


        }
    }
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

flysh05

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

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

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

打赏作者

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

抵扣说明:

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

余额充值