原原本本從書上搬進去的代碼,運行調試卻不成功!哪位高手幫看一下?代碼如下:using System;using System.Collections.Generic;using System.Text;namespace interfaceExample{public interface IBank{void PayIn(float count);bool withdraw(float count);float Transfercount{get;}}public interface ITransferBankAccount : IBank{bool TransferTo(IBank destination,float amount);}public class CurrentAccount : ITransferBankAccount{private float balance;public void PanIn(float amount){balance += amount;}public bool withdraw(float count){if (balance >= count){balance -= count;return true;}Console.WriteLine("賬面出錯了!");return false;}public float Transfercount{get{return balance;}}public bool TransferTo(IBank destination, float amount){bool result;if ((result = withdraw(amount)) == true)destination.PayIn(amount);return result;}public override string ToString(){return string.Format("當前賬目結余={0,6:c}",balance);}}public class BankA : IBank{private float transfercount;public BankA(){Console.WriteLine("繼承自IBank的BankA");}public void PayIn(float count){transfercount += count;}public bool withdraw(float count){if (transfercount >= count){transfercount -= count;return true;}Console.WriteLine("賬面出錯了!");return false;}public float Transfercount{get{return transfercount;}}public override string ToString(){return string.Format("Bank 結余:Transfercount={0,6:c}", transfercount);}}class Program{static void Main(string[] args){IBank Account = new BankA();ITransferBankAccount AccountB = new CurrentAccount();Account.PayIn(200);AccountB.PayIn(500);AccountB.TransferTo(Account,100);Console.WriteLine(Account.ToString());Console.WriteLine(AccountB.ToString());}}}
2 回答

幕布斯7119047
TA貢獻1794條經驗 獲得超8個贊
是你寫錯字符了哈。
你在接口中是這樣定義的:
public interface IBank
{
void PayIn(float count);//注意:PayIn
bool withdraw(float count);
float Transfercount{get;}
}
而你在類中是這樣寫的:
public class CurrentAccount:ITransferBankAccount
{
private float balance;
public void PanIn(float amount)//顯然不能繼承了
//改成 PayIn就正確了
{
balance += amount;
}
.....
}
由于接口的內部結構不一樣,所以就不能繼承了噻。
但是你的程序應該還有問題:
public override string ToString()
{
return string.Format("當前賬目結余={0,6:c}", balance);
}
你看看是不是格式的轉換上有問題。

神不在的星期二
TA貢獻1963條經驗 獲得超6個贊
錯誤 1
“interfaceExample .CurrentAccount”不會實現接口成員“interfaceExample .IBank.PayIn(float)”
- 2 回答
- 0 關注
- 333 瀏覽
添加回答
舉報
0/150
提交
取消