C#

代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace 接口
  8 {
  9     /// <summary>
 10     /// 接口:定义一个统一的标准
 11     /// 声明接口,接口中,只包含成员的生命,不包含任何的代码实现。
 12     /// 接口成员总是公有的,不添加也不需要添加public,也不能声明虚方法和虚静态方法
 13     /// </summary>
 14     interface IBankAccount
 15     {
 16         //方法
 17         void PayIn(decimal amount);
 18 
 19         //方法
 20         bool WithShowMyself(decimal amount);
 21 
 22         //属性
 23         decimal Balance { get; }
 24     }
 25 
 26     /// <summary>
 27     /// 继承自IBankAccount的接口
 28     /// </summary>
 29     interface ITransferBankAccount : IBankAccount
 30     {
 31         //转账          :转入的目的地             :转入金额
 32         bool TransferTo(IBankAccount destination, decimal amount);
 33     }
 34 
 35     class SaveAcount : IBankAccount
 36     {
 37         //私有变量
 38         private decimal banlance;
 39 
 40         //存款
 41         public void PayIn(decimal amount)
 42         {
 43             banlance += amount;
 44         }
 45 
 46         //取款
 47         public bool WithShowMyself(decimal amount)
 48         {
 49             if (banlance >= amount)
 50             {
 51                 banlance -= amount;
 52                 return true;
 53             }
 54             else
 55             {
 56                 Console.WriteLine("余额不足!");
 57                 return false;
 58             }
 59         }
 60 
 61         //账户余额
 62         public decimal Balance
 63         {
 64             get
 65             {
 66                 return banlance;
 67             }
 68         }
 69     }
 70 
 71     //实现接口的类的相应成员必须添加public修饰
 72     class ITransferAccount : ITransferBankAccount
 73     {
 74         //私有变量
 75         private decimal banlance;
 76 
 77         //存款
 78         public void PayIn(decimal amount)
 79         {
 80             banlance += amount;
 81         }
 82 
 83         //取款
 84         public bool WithShowMyself(decimal amount)
 85         {
 86             if (banlance >= amount)
 87             {
 88                 banlance -= amount;
 89                 return true;
 90             }
 91             else
 92             {
 93                 Console.WriteLine("余额不足!");
 94                 return false;
 95             }
 96         }
 97 
 98         //账户余额
 99         public decimal Balance
100         {
101             get
102             {
103                 return banlance;
104             }
105         }
106 
107         //转账
108         public bool TransferTo(IBankAccount destination, decimal amount)
109         {
110             bool result = WithShowMyself(amount);
111 
112             if (result == true)
113             {
114                 destination.PayIn(amount);
115             }
116 
117             return result;
118         }
119     }
120 
121 
122     class Program
123     {
124         static void Main(string[] args)
125         {
126             IBankAccount MyAccount = new SaveAcount();
127 
128             ITransferAccount YourAccount = new ITransferAccount();
129 
130             MyAccount.PayIn(10000);
131 
132             YourAccount.PayIn(30000);
133 
134             YourAccount.TransferTo(MyAccount, 5000);
135 
136             Console.WriteLine(MyAccount.Balance);//15000
137             Console.WriteLine();
138             Console.WriteLine(YourAccount.Balance);//25000
139 
140             Console.ReadKey();
141         }
142     }
143 }
原文地址:https://www.cnblogs.com/KTblog/p/4525837.html