欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于c# 接口的实例详解

程序员文章站 2024-04-03 08:03:52
复制代码 代码如下:namespace consoleapplication1{    using system;  &n...
复制代码 代码如下:

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);
    }
}