初学者急求:不会实现接口成员
代码如下:
using System.Collections.Generic;
using System.Text;
using Wrox.ProCSharp;
using Wrox.ProCSharp.VenusBank;
using Wrox.ProCSharp.JupiterBank;
namespace Wrox.ProCSharp
{
public interface lBankAccount
{
void PayIn(decimal amount);
bool WithDraw(decimal amount);
decimal Balance
{
get;
}
}
}
namespace Wrox.ProCSharp.VenusBank
{
public class SaverAccount : lBankAccount
{
private decimal banlance;
public void PayIn(decimal amount)
{
banlance += amount;
}
public bool Withdraw(decimal amount)
{
if(banlance>=amount)
{
banlance -= amount ;
return true;
}
Console.WriteLine("withDraw attempt failed");
return false;
}
public decimal Banlance
{
get
{
return banlance;
}
}
public override string ToString()
{
return string.Format("Verus Bank Saver:Balance={0,6:C}",banlance);
}
}
}
namespace Wrox.ProCSharp.JupiterBank
{
public class GoldAccount : lBankAccount
{
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
lBankAccount venusAccount = new SaverAccount();
lBankAccount jupiterAccount = new GoldAccount();
venusAccount.PayIn(200);
venusAccount.WithDraw(100);
Console.WriteLine(venusAccount.ToString());
jupiterAccount.PayIn(500);
jupiterAccount.WithDraw(600);
jupiterAccount.WithDraw(100);
Console.WriteLine(jupiterAccount.ToString());
}
}
}
出错:
错误1“Wrox.ProCSharp.VenusBank.SaverAccount”不会实现接口成员“Wrox.ProCSharp.lBankAccount.WithDraw(decimal)”d:\wody\Projects\form\ConsoleApplication1\ConsoleApplication1\Program.cs2418ConsoleApplication1
错误2“Wrox.ProCSharp.VenusBank.SaverAccount”不会实现接口成员“Wrox.ProCSharp.lBankAccount.Balance”d:\wody\Projects\form\ConsoleApplication1\ConsoleApplication1\Program.cs2418ConsoleApplication1
错误3“Wrox.ProCSharp.JupiterBank.GoldAccount”不会实现接口成员“Wrox.ProCSharp.lBankAccount.PayIn(decimal)”d:\wody\Projects\form\ConsoleApplication1\ConsoleApplication1\Program.cs6318ConsoleApplication1
错误4“Wrox.ProCSharp.JupiterBank.GoldAccount”不会实现接口成员“Wrox.ProCSharp.lBankAccount.WithDraw(decimal)”d:\wody\Projects\form\ConsoleApplication1\ConsoleApplication1\Program.cs6318ConsoleApplication1
错误5“Wrox.ProCSharp.JupiterBank.GoldAccount”不会实现接口成员“Wrox.ProCSharp.lBankAccount.Balance”d:\wody\Projects\form\ConsoleApplication1\ConsoleApplication1\Program.cs6318ConsoleApplication1
[解决办法]
using System.Collections.Generic;
using System.Text;
using Wrox.ProCSharp;
using Wrox.ProCSharp.VenusBank;
using Wrox.ProCSharp.JupiterBank;
namespace Wrox.ProCSharp
{
public interface lBankAccount
{
void PayIn(decimal amount);
bool WithDraw(decimal amount);
decimal Balance
{
get;
}
}
}
namespace Wrox.ProCSharp.VenusBank
{
public class SaverAccount : lBankAccount
{
private decimal banlance;
public void PayIn(decimal amount)
{
banlance += amount;
}
public bool Withdraw(decimal amount)
{
if(banlance> =amount)
{
banlance -= amount ;
return true;
}
Console.WriteLine("withDraw attempt failed");
return false;
}
public decimal Banlance
{
get
{
return banlance;
}
}
public override string ToString()
{
return string.Format("Verus Bank Saver:Balance={0,6:C}",banlance);
}
}
}
namespace Wrox.ProCSharp.JupiterBank
{
public class GoldAccount : lBankAccount
{
这里没有实现lBankAccount 接口
private decimal banlance;
public void PayIn(decimal amount)
{
banlance += amount;
}
public bool Withdraw(decimal amount)
{
if(banlance> =amount)
{
banlance -= amount ;
return true;
}
Console.WriteLine("withDraw attempt failed");
return false;
}
public decimal Banlance
{
get
{
return banlance;
}
}
public override string ToString()
{
return string.Format("Verus Bank Saver:Balance={0,6:C}",banlance);
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
lBankAccount venusAccount = new SaverAccount();
lBankAccount jupiterAccount = new GoldAccount();
venusAccount.PayIn(200);
venusAccount.WithDraw(100);
Console.WriteLine(venusAccount.ToString());
jupiterAccount.PayIn(500);
jupiterAccount.WithDraw(600);
jupiterAccount.WithDraw(100);
Console.WriteLine(jupiterAccount.ToString());
}
}
}
[解决办法]
这是个明显的低级错误!!!!!!!!!!!!!!!!!!!
要求实现的那两个东西
bool WithDraw(decimal amount);
decimal Balance
{
get;
}
一个是被你写成了Withdraw (应该是WithDraw)
另一个是被你写成了Banlance (应该是Balance)
还有
你根本就没有导入 System 命名空间,就写上了Console.WriteLine
[解决办法]
public class SaverAccount : IBankAccount { void IBankAccount.PayIn(decimal amount) //必须写明是实现哪个接口的方法 { //省略 } bool IBankAccount.Withdraw(decimal amount) { //省略 } decimal IBankAccount.Banlance { //省略 } }