C#高级编程学习笔记--------派生的接口
来源:互联网 发布:淘宝规蜜怎么进入 编辑:程序博客网 时间:2024/06/04 19:42
using System;
using System.Collections.Generic;
using System.Text;
using Wrox.ProCSharp.VenusBank;
namespace Wrox.ProCSharp
...{
public interface IBankAccount
...{
void PayIn(decimal amount); //存款的方法
bool Withdraw(decimal amount); //判断是否余款不足
decimal Balance //返回余款的属性
...{
get;
}
}
public interface ITransferBankAccount : IBankAccount
...{
bool TransferTo(IBankAccount destination, decimal amount);
}
public class XufangweiAccount : ITransferBankAccount
...{
//实现接口
private decimal balance;
public void PayIn(decimal amount)
...{
balance += amount;
}
public bool Withdraw(decimal amount)
...{
if (balance >= amount)
...{
balance -= amount;
return true;
}
Console.WriteLine("转帐失败");
return false;
}
public decimal Balance
...{
get
...{
return balance;
}
}
public bool TransferTo(IBankAccount destination, decimal amount)
...{
bool result;
if ((result = Withdraw(amount))==true)
destination.PayIn(amount);
return result;
}
public override string ToString()
...{
return String.Format("xfw Bank Current Account: Balance = {0,6:C}", balance);
}
}
class MainEntryPoint
...{
static void Main()
...{
IBankAccount cyyAccount = new ChenyeyeAccount(); //以上已经详细说明表示他们可以指向实现这些接口的任何方法
ITransferBankAccount xfwAccount = new XufangweiAccount();
cyyAccount.PayIn(200);
xfwAccount.PayIn(500);
xfwAccount.TransferTo(cyyAccount, 100);
Console.WriteLine(cyyAccount.ToString());
Console.WriteLine(xfwAccount.ToString());
}
}
}
namespace Wrox.ProCSharp.VenusBank
...{
public class ChenyeyeAccount : IBankAccount
...{
private decimal balance;
public void PayIn(decimal amount)
...{
balance += amount;
}
public bool Withdraw(decimal amount)
...{
if (balance >= amount)
...{
balance -= amount;
return true;
}
Console.WriteLine("转帐失败.");
return false;
}
public decimal Balance
...{
get
...{
return balance;
}
}
public override string ToString()
...{
return String.Format("cyy Bank Saver: Balance = {0,6:C}", balance);
}
}
}
/**//*总结:
* 这也是一个银行转帐的东东,不同的是转入转出比先前多了一个帐户信息,刚才的例子只是帐户之间钱的增加和减少,
* 为了增加转帐户信息,这里派生了一个接口
* public interface ITransferBankAccount : IBankAccount
{
bool TransferTo(IBankAccount destination, decimal amount);
}
* 该接口的功能与IBankAccount相同,只是又定义了一个方法,把资金直接转到另一个账户上。
*
* 注意:::
* ITransferBankAccount派生于IBankAccount,所以拥有IBankAccount的所有成员和它自己的成员。
* 这表示执行(派生于)ITransferBankAccount的任何类都必须执行IBankAccount的所有方法和在ITransferBankAccount
* 中定义的新方法TransferTo()。没有执行所有这些方法就会出现编译错误:如:“不会实现接口成员”
*/
using System.Collections.Generic;
using System.Text;
using Wrox.ProCSharp.VenusBank;
namespace Wrox.ProCSharp
...{
public interface IBankAccount
...{
void PayIn(decimal amount); //存款的方法
bool Withdraw(decimal amount); //判断是否余款不足
decimal Balance //返回余款的属性
...{
get;
}
}
public interface ITransferBankAccount : IBankAccount
...{
bool TransferTo(IBankAccount destination, decimal amount);
}
public class XufangweiAccount : ITransferBankAccount
...{
//实现接口
private decimal balance;
public void PayIn(decimal amount)
...{
balance += amount;
}
public bool Withdraw(decimal amount)
...{
if (balance >= amount)
...{
balance -= amount;
return true;
}
Console.WriteLine("转帐失败");
return false;
}
public decimal Balance
...{
get
...{
return balance;
}
}
public bool TransferTo(IBankAccount destination, decimal amount)
...{
bool result;
if ((result = Withdraw(amount))==true)
destination.PayIn(amount);
return result;
}
public override string ToString()
...{
return String.Format("xfw Bank Current Account: Balance = {0,6:C}", balance);
}
}
class MainEntryPoint
...{
static void Main()
...{
IBankAccount cyyAccount = new ChenyeyeAccount(); //以上已经详细说明表示他们可以指向实现这些接口的任何方法
ITransferBankAccount xfwAccount = new XufangweiAccount();
cyyAccount.PayIn(200);
xfwAccount.PayIn(500);
xfwAccount.TransferTo(cyyAccount, 100);
Console.WriteLine(cyyAccount.ToString());
Console.WriteLine(xfwAccount.ToString());
}
}
}
namespace Wrox.ProCSharp.VenusBank
...{
public class ChenyeyeAccount : IBankAccount
...{
private decimal balance;
public void PayIn(decimal amount)
...{
balance += amount;
}
public bool Withdraw(decimal amount)
...{
if (balance >= amount)
...{
balance -= amount;
return true;
}
Console.WriteLine("转帐失败.");
return false;
}
public decimal Balance
...{
get
...{
return balance;
}
}
public override string ToString()
...{
return String.Format("cyy Bank Saver: Balance = {0,6:C}", balance);
}
}
}
/**//*总结:
* 这也是一个银行转帐的东东,不同的是转入转出比先前多了一个帐户信息,刚才的例子只是帐户之间钱的增加和减少,
* 为了增加转帐户信息,这里派生了一个接口
* public interface ITransferBankAccount : IBankAccount
{
bool TransferTo(IBankAccount destination, decimal amount);
}
* 该接口的功能与IBankAccount相同,只是又定义了一个方法,把资金直接转到另一个账户上。
*
* 注意:::
* ITransferBankAccount派生于IBankAccount,所以拥有IBankAccount的所有成员和它自己的成员。
* 这表示执行(派生于)ITransferBankAccount的任何类都必须执行IBankAccount的所有方法和在ITransferBankAccount
* 中定义的新方法TransferTo()。没有执行所有这些方法就会出现编译错误:如:“不会实现接口成员”
*/
- C#高级编程学习笔记--------派生的接口
- c#高级编程之派生的接口
- C#高级编程学习笔记--------接口的定义和实现
- C#高级编程 学习笔记
- C#2005 .NET3.0高级编程学习笔记————接口
- C#高级编程学习笔记(1)
- C#高级编程学习笔记--------构造函数
- C#高级编程第四版学习笔记
- C#高级编程学习笔记(LINQ)
- C#高级编程学习笔记(委托)
- 《C#高级编程》【第二章】核心C#编程 -- 学习笔记
- 《C#高级编程》笔记
- C# ErrorsAndExceptions- 错误与异常的学习-《C#高级编程》学习笔记
- 《C#高级编程》做的笔记
- C#高级编程学习笔记(一)---C#核心
- C#高级编程 学习笔记(一) 第一个C#程序
- C#高级编程学习笔记--------构造函数(1)
- C#高级编程学习笔记--------内存管理和指针
- 由ORM(Object Relation Mapping,对象关系映像)所想的
- 感动 - [人生随笔]2007-06-07
- 怎么用nc在两个机器中间传文件
- 感动 - [人生随笔]2007-06-07
- MSDN 书屋
- C#高级编程学习笔记--------派生的接口
- delphi tips 6
- Programming Windows程式开发设计指南->第三章 窗口和消息
- 关于f:/system volume information
- 大学要做的50件事
- Programming Windows程式开发设计指南->第三章 例子程序
- 将Excel的数据导入到Sql数据库
- Struts中文乱码解决实例(绝对经典)
- Java程序员学C++_1_C++中的预处理命令