代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 接口
{
/// <summary>
/// 接口:定义一个统一的标准
/// 声明接口,接口中,只包含成员的生命,不包含任何的代码实现。
/// 接口成员总是公有的,不添加也不需要添加public,也不能声明虚方法和虚静态方法
/// </summary>
interface IBankAccount
{
//方法
void PayIn(decimal amount); //方法
bool WithShowMyself(decimal amount); //属性
decimal Balance { get; }
} /// <summary>
/// 继承自IBankAccount的接口
/// </summary>
interface ITransferBankAccount : IBankAccount
{
//转账 :转入的目的地 :转入金额
bool TransferTo(IBankAccount destination, decimal amount);
} class SaveAcount : IBankAccount
{
//私有变量
private decimal banlance; //存款
public void PayIn(decimal amount)
{
banlance += amount;
} //取款
public bool WithShowMyself(decimal amount)
{
if (banlance >= amount)
{
banlance -= amount;
return true;
}
else
{
Console.WriteLine("余额不足!");
return false;
}
} //账户余额
public decimal Balance
{
get
{
return banlance;
}
}
} //实现接口的类的相应成员必须添加public修饰
class ITransferAccount : ITransferBankAccount
{
//私有变量
private decimal banlance; //存款
public void PayIn(decimal amount)
{
banlance += amount;
} //取款
public bool WithShowMyself(decimal amount)
{
if (banlance >= amount)
{
banlance -= amount;
return true;
}
else
{
Console.WriteLine("余额不足!");
return false;
}
} //账户余额
public decimal Balance
{
get
{
return banlance;
}
} //转账
public bool TransferTo(IBankAccount destination, decimal amount)
{
bool result = WithShowMyself(amount); if (result == true)
{
destination.PayIn(amount);
} return result;
}
} class Program
{
static void Main(string[] args)
{
IBankAccount MyAccount = new SaveAcount(); ITransferAccount YourAccount = new ITransferAccount(); MyAccount.PayIn(); YourAccount.PayIn(); YourAccount.TransferTo(MyAccount, ); Console.WriteLine(MyAccount.Balance);//
Console.WriteLine();
Console.WriteLine(YourAccount.Balance);// Console.ReadKey();
}
}
}