C#高级编程学习笔记--------派生的接口

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()。没有执行所有这些方法就会出现编译错误:如:“不会实现接口成员”
 
 
 
*/
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值