加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

基于c# 接口的实例详解

发布时间:2020-12-15 06:00:38 所属栏目:百科 来源:网络整理
导读:复制代码 代码如下: namespace ConsoleApplication1 { using System; using System.Collections.Generic; using System.Text; public class BankMethod : IBankAccount { decimal balance; public void PayIn(decimal Account) { balance += Account; //Cons
复制代码 代码如下:

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    public class BankMethod : IBankAccount
    {
        decimal balance;
        public void PayIn(decimal Account)
        {
            balance += Account;
            //Console.WriteLine("您现在的存款是:{0}",balance);
        }
        public bool PayOut(decimal Account)
        {
            if (Balance > Account)
            {
                balance -= Account;
                Console.WriteLine("您已经取走了{0},还剩下余额是:{1}",Account,balance);
                return true;
            }
            Console.WriteLine("提款失败!");
            return false;
        }
        public decimal Balance
        {
            get { return balance; }
        }
        public override string ToString()
        {
            return string.Format("您现在的存款是:{0:C}",balance);
        }
    }
    class Test
    {
        static void Main()
        {
            IBankAccount Huguo = new BankMethod();
            IBankAccount guo = new BankMethod();
            Huguo.PayIn(10000);
            guo.PayIn(200000);
            Console.WriteLine(Huguo.ToString());
            Console.WriteLine(guo.ToString());
            //BankMethod Bank = new BankMethod();
            //Bank.PayIn(200000);
            //Bank.PayOut(30000);
        }
    }
}

复制代码 代码如下:

namespace ConsoleApplication1
{
    public interface IBankAccount
    {
        void PayIn(decimal amount);
        bool PayOut(decimal amount);
        decimal Balance
        {
            get;
        }
    }
    public interface IBankTransfer:IBankAccount
    {
        bool Transfer(IBankAccount Action,decimal amount);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读