不兼容的返回类型错误java
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
我做错了什么?
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; }
public void setAccountNumber(int accountnumber) { return accountnumber; }
这里你的返回类型是void
,你的返回int
。
public void setAccountNumber(int accountnumber) { return accountnumber; } public void deposit(double amount) { return this.accountbalance=this.accountbalance+amount; }
…你试图返回void
-return函数???
这是正确的:
public int setAccountNumber(int accountnumber) { // ^^^ return accountnumber; } public double deposit(double amount) { // ^^^^^^ return this.accountbalance=this.accountbalance+amount; }
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 }
到目前为止,每个答案都是正确的,并指出了问题 这是一个尚未提及的。
您的equals
方法没有正确覆盖父级:
public boolean equals(BankAccount ba) { return this.accountbalance == ba.accountbalance; }
Object
上的equals
签名是public boolean equals(Object other)
因此任何对象都可以相互比较。 您已将其更改为仅允许与违反合同的其他BankAccounts进行比较。
试试这个:
public boolean equals(BankAccount ba) { if (ba instanceof BankAccount) { return this.accountbalance == ((BankAccount) ba).accountbalance; } else { return false; } }