【发布时间】:2014-08-17 16:45:29
【问题描述】:
我在 C# 教程中收到错误 Cannot create an instance of the abstract class or interface。
这条线失败了:result = new Account(nameText, addressText, balance);
这是我的课:
public abstract class Account : IAccount
{
//---------------------------------------------------------------
// Constructor
//---------------------------------------------------------------
public Account(string inName, string inAddress, decimal inBalance)
{
name = inName;
address = inAddress;
balance = inBalance;
}
public Account(string inName, string inAddress) :
this(inName, inAddress, 0) // 'this ties this alternate constructor back to the original constructor (directly above)
{
}
public Account(string inName) : // 'this ties this alternate constructor back to the original constructor (directly above)
this(inName, "Not Supplied", 0)
{
}
//---------------------------------------------------------------
// Properties
//---------------------------------------------------------------
// * * * * * * * *
//global account constraints
private static decimal minIncome = 10000;
private static int minAge = 18;
// * * * * * * * *
//personal details
private string name;
private string address;
// * * * * * * * *
//account details
public int AccountNumber;
public static decimal InterestRateCharged;
public AccountState State;
private decimal balance = 0;
public int Overdraft;
//---------------------------------------------------------------
// Methods
//---------------------------------------------------------------
// loads the account
public static Account Load(string filename)
{
Account result = null;
System.IO.TextReader textIn = null;
try
{
textIn = new System.IO.StreamReader(filename);
string nameText = textIn.ReadLine();
string addressText = textIn.ReadLine();
string balanceText = textIn.ReadLine();
decimal balance = decimal.Parse(balanceText);
result = new Account(nameText, addressText, balance);
}
catch
{
return null;
}
finally
{
if (textIn != null) textIn.Close();
}
return result;
}
};
这是我的界面:
public interface IAccount
{
// account info
int GetAccountNumber();
string GetName();
decimal GetBalance();
// account actions
void PayInFunds(decimal amount);
bool WithdrawFunds(decimal amount);
string RudeLetterString();
}
【问题讨论】:
-
如果要实例化这个类为什么是抽象的?
-
错误的哪一部分你不明白?
-
您确定您甚至阅读了错误消息吗?即使您不这样做,您也可以将其直接复制并粘贴到谷歌中,这将为您提供大量结果,准确解释它是什么、为什么会发生以及如何解决它。