因此,我正在开发该程序,并创建了两个类,一个类称为CreditCard,另一个类称为CreditCardTester。我使用的是Eclipse IDE,并且不断出现编译错误,例如“类型中的方法getBalance(double)信用卡不适用于参数()“。我不确定要解决的问题。
这是第一类,称为CreditCard:
public class CreditCard
{
private String accountNumber;
private double creditLimit;
private double balance;
public void CreditCard(String number, double limit)
{
accountNumber = number;
limit = creditLimit;
balance = 0;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getCreditLimit()
{
return creditLimit;
}
public double getBalance(double theBalance)
{
return balance;
}
public double charge(double amount)
{
balance = balance + amount;
return balance;
}
这是第二类CreditCardTester:
public class CreditCardTester
{
public static void main(String[] args)
{
CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
String formatString = "Credit Card [number = %s, bal = %.2f, limit = %.2f]\n";
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
System.out.println("\nCharging $50.00 to credit card...\n");
card.charge(50.00);
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
}
最佳答案
这不是构造函数,因为您添加了void
,使其成为常规方法:
public void CreditCard(String number, double limit)
删除
void
:public CreditCard(String number, double limit)
同样,方法/构造函数中的分配之一是向后的。您已将实例变量分配给了参数。
limit = creditLimit;
改变它:
creditLimit = limit;
创建
CreditCard
时,您缺少“new”:CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
尝试
CreditCard card = new CreditCard("1234-5678-9012-3456", 1000.00);
您在
getBalance
方法上有一个未使用的参数,并且在不带参数的情况下调用它。public double getBalance(double theBalance)
去掉它:
public double getBalance()
关于java - Eclipse上的Java编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21146727/