1. 什么是接口
- 在软件领域,实现标准化的一种方法是制定统一的接口(Interface)。
- 比如许多银行会编写自己的账户系统,但他们都赞同使用相同的接口,以便在不同银行之间进行转帐业务。
- 接口只规定系统具有哪些成员以及每个成员的功能,具体如何实现由各个公司自己设计,也就是说接口为大家制定了一个规范,然后各公司具体实现这个规范。
2. 银行账户类——实现接口
现在我们编写一个实现了 IBankAccount 接口的银行账户类,不同的银行可以有不同的实现方法,但必须实现 IBankAccount 接口中的所有成员。
using System;
namespace BankAccount
{
class Program
{
static void Main(string[] args)
{
//我们用 IBankAccount 接口的引用符 myAccount 指向了SaverAccount 类的对象。
//实际上,如果有多个类派生于 IBankAccount 接口,接口引用符myAccount 可以指向所有派生类的对象,这体现了接口的多态性。
IBankAccount myAccount = new SaverAccount();
myAccount.PayIn(1000);
myAccount.Withdraw(200);
Console.WriteLine("余额:{0}", myAccount.Balance);
}
}
#region 定义接口
//接口用关键字 interface 定义,接口的名称习惯上以字母 I 开头。
//接口中只能包含成员的声明,不能有任何实现代码。
//接口的成员总是公有的,不需要也不能添加public等修饰符,也不能声明为虚方法或静态方法。
interface IBankAccount
{
//方法:存款
void PayIn(decimal amount);
//方法:取款
bool Withdraw(decimal amount);
//方法:账户余额
decimal Balance { get; }
}
#endregion
#region 实现接口的类
//SaverAccount类中实现了IBankAccount接口的所有成员
//注意,实现接口的类的相应成员必须添加 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;
}
else
{
Console.WriteLine("余额不足,取款失败。");
return false;
}
}
//方法:余额
public decimal Balance
{
get
{
return balance;
}
}
}
#endregion
}
3. 可转账的银行账户——接口的继承
using System;
namespace BankAccount
{
class Program
{
static void Main(string[] args)
{
//我们用 IBankAccount 接口的引用符 myAccount 指向了SaverAccount 类的对象。
//实际上,如果有多个类派生于 IBankAccount 接口,接口引用符myAccount 可以指向所有派生类的对象,这体现了接口的多态性。
IBankAccount myAccount = new SaverAccount();
myAccount.PayIn(1000);
myAccount.Withdraw(200);
Console.WriteLine("余额:{0}", myAccount.Balance);
IBankAccount myAccount1 = new SaverAccount();
ITransferBankAccount yourAccount1 = new TransferAccount();
myAccount1.PayIn(10000);
yourAccount1.PayIn(30000);
//你向我转 5000 元
yourAccount1.TransferTo(myAccount, 5000);
Console.WriteLine("我的余额:{0}", myAccount1.Balance);
Console.WriteLine("你的余额:{0}", yourAccount1.Balance);
}
}
#region 定义接口1
//接口用关键字 interface 定义,接口的名称习惯上以字母 I 开头。
//接口中只能包含成员的声明,不能有任何实现代码。
//接口的成员总是公有的,不需要也不能添加public等修饰符,也不能声明为虚方法或静态方法。
interface IBankAccount
{
//方法:存款
void PayIn(decimal amount);
//方法:取款
bool Withdraw(decimal amount);
//方法:账户余额
decimal Balance { get; }
}
#endregion
#region 定义接口2 (继承于接口1)
//ITransferBankAccount 接口继承了 IBankAccount 接口的所有成员,并且定义了一个新成员方法 TransferTo()。
interface ITransferBankAccount : IBankAccount
{
//方法:转帐
//注意 TransferTo()方法的第一个参数,其类型为 IBankAccount,
//它巧妙地运用了接口的多态性,不管目标账户是什么类,只要这个类继承于 IBankAccount接口即可。
bool TransferTo(IBankAccount destination, decimal amount);
}
#endregion
#region 实现接口1的类
//SaverAccount类中实现了IBankAccount接口的所有成员
//注意,实现接口的类的相应成员必须添加 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;
}
else
{
Console.WriteLine("余额不足,取款失败。");
return false;
}
}
//方法:余额
public decimal Balance
{
get
{
return balance;
}
}
}
#endregion
#region 实现接口2的类
class TransferAccount : ITransferBankAccount
{
//私有变量:余额
private decimal balance;
//方法:存款
public void PayIn(decimal amount)
{
balance += amount;
}
//方法:取款
public bool Withdraw(decimal amount)
{
if (balance >= amount)
{
balance -= amount;
return true;
}
else
{
Console.WriteLine("余额不足,取款失败。");
return false;
}
}
//属性:账户余额
public decimal Balance
{
get
{
return balance;
}
}
//方法:转账
public bool TransferTo(IBankAccount destination, decimal amount)
{
bool result = Withdraw(amount);
if (result == true)
{
destination.PayIn(amount);
}
return result;
}
}
#endregion
}
4. 接口的多继承和显示实现
- 在 C#中,类与类之间是单继承的,即一个类只能继承一个基类。但类与接口之间是多继承的,一个类可以实现多个接口。
using System;
namespace Breath
{
class Program
{
static void Main(string[] args)
{
IAnimal life1 = new Life();
IPlant life2 = new Life();
life1.Breathe();
life2.Breathe();
}
interface IAnimal
{
void Breathe();
}
public interface IPlant
{
void Breathe();
}
//该类继承了两个接口
class Life : IAnimal, IPlant
{
void IAnimal.Breathe()
{
Console.WriteLine("我实现了接口IAnimal的Breathe方法。");
}
void IPlant.Breathe()
{
Console.WriteLine("我实现了接口IPlant的Breathe方法。");
}
}
}