class BankAccount {
private String firstname;
private String lastname;
private int ssn;
private int accountnumber = 0;
private double accountbalance = 0.0;
BankAccount() {
firstname = "John Smith";
}
BankAccount(String firstname, String lastname, int ssn) {
int accountnumber;
double accountbalance;
}
public String getName() {
return firstname;
}
public double getBalance() {
return accountbalance;
}
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
public boolean equals(BankAccount ba) {
return this.accountbalance == ba.accountbalance;
}
public void deposit(double amount) {
return this.accountbalance = this.accountbalance + amount;
}
public int withdraw(double amount) {
this.accountbalance = this.accountbalance - amount;
if(accountbalance < 0) {
this.accountbalance = this.accountbalance + amount;
return -1;
}
else
return 0;
}
public String toString() {
return firstname+lastname+"/n"+"social secuirty "+"ssn"+"/n"+"account number is "+
"accountnumber" +"/n" +"Balance:" +"accountbalance" +"/n";
}
}
错误是:
BankAccount.java:34: error: incompatible types: unexpected return value
return accountnumber;
^
BankAccount.java:47: error: incompatible types: unexpected return value
return this.accountbalance=this.accountbalance+amount;
^
2 errors
我做错了什么?
答案 0 :(得分:6)
void
方法无法return
。并且具有返回类型的方法必须return
方法签名中指定的类型。
错误1
例如,查看此方法
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
哟无法从void
方法返回。
那应该是
public void setAccountNumber(int accountnumber) {
this.accountnumber =accountnumber;
}
其他方法也是如此。
错误2
public void deposit(double amount) {
return this.accountbalance = this.accountbalance + amount;
}
返回语句在语法上是错误的。你无法返回,因为它是无效的。那应该是
public void deposit(double amount) {
this.accountbalance = this.accountbalance + amount;
}
答案 1 :(得分:2)
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
此处您的返回类型为void
,而您的返回int
。
答案 2 :(得分:0)
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
public void deposit(double amount) {
return this.accountbalance=this.accountbalance+amount;
}
......你是否试图返回void
- 返回功能???
这是正确的:
public int setAccountNumber(int accountnumber) {
// ^^^
return accountnumber;
}
public double deposit(double amount) {
// ^^^^^^
return this.accountbalance=this.accountbalance+amount;
}
答案 3 :(得分:0)
Forst Error是meyhod的返回类型为void
,方法返回int
。
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
将其更改为:
public int setAccountNumber(int accountnumber) {
return accountnumber;
}
,第二件事是这对于用于获取变量的方法来说不是一个好名字,将方法名称更改为:
public int getAccountNumber(int accountnumber) { // why is this argument???
return accountnumber;
}
还有一件事,你的方法
public void deposit(double amount) {
return this.accountbalance = this.accountbalance + amount;
}
有同样的错误。它应该是:
public double deposit(double amount) { //change void to double
return this.accountbalance + amount; //as suggested by SURESH ATTA
}
答案 4 :(得分:0)
到目前为止,每个答案都是正确的,并指出了问题。这是一个尚未提及的。
您的equals
方法无法正确覆盖父级:
public boolean equals(BankAccount ba) {
return this.accountbalance == ba.accountbalance;
}
equals
上Object
的签名为public boolean equals(Object other)
,以便可以将任何对象相互比较。您已将其更改为仅允许与违反合同的其他BankAccounts进行比较。
请改为尝试:
public boolean equals(BankAccount ba) {
if (ba instanceof BankAccount) {
return this.accountbalance == ((BankAccount) ba).accountbalance;
}
else {
return false;
}
}