亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您好,如果C#接口繼承例子調試不成功,該怎么辦呢?

您好,如果C#接口繼承例子調試不成功,該怎么辦呢?

C# C
暮色呼如 2021-11-19 15:11:08
原原本本從書上搬進去的代碼,運行調試卻不成功!哪位高手幫看一下?代碼如下: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);
}
你看看是不是格式的轉換上有問題。



查看完整回答
反對 回復 2021-11-24
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

錯誤 1
“interfaceExample .CurrentAccount”不會實現接口成員“interfaceExample .IBank.PayIn(float)”

查看完整回答
反對 回復 2021-11-24
  • 2 回答
  • 0 關注
  • 333 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號