为了账号安全,请及时绑定邮箱和手机立即绑定

您好,如果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 关注
  • 280 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信