您好,如果C#接口继承例子调试不成功,该怎么办呢?

原原本本从书上搬进去的代码,运行调试却不成功!哪位高手帮看一下?
代码如下:
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());
}
}
}

暮色呼如
浏览 281回答 2
2回答

幕布斯7119047

是你写错字符了哈。你在接口中是这样定义的:public interface IBank{void PayIn(float count);//注意:PayInbool 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);}你看看是不是格式的转换上有问题。

神不在的星期二

错误 1“interfaceExample .CurrentAccount”不会实现接口成员“interfaceExample .IBank.PayIn(float)”
打开App,查看更多内容
随时随地看视频慕课网APP