不兼容的返回类型错误java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25037558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Incompatible return type error java
提问by higgs
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";
}
}
Error is :
错误是:
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
What am I doint wrong?
我做错了什么?
回答by Suresh Atta
A void
method cannot return
someting. And a method which have a return type must return
the type specified in method signature.
一个void
方法不能return
something。并且具有返回类型的方法必须return
是方法签名中指定的类型。
Error 1
错误 1
For example look this method
例如看这个方法
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
Yo cannot return from a void
method.
你不能从void
方法返回。
That should be
那应该是
public void setAccountNumber(int accountnumber) {
this.accountnumber =accountnumber;
}
Same goes for remaining methods too.
其余方法也是如此。
Error 2
错误 2
public void deposit(double amount) {
return this.accountbalance = this.accountbalance + amount;
}
that return statement is syntactically wrong. You cannot return as it is void. That should be
该 return 语句在语法上是错误的。您无法返回,因为它是无效的。那应该是
public void deposit(double amount) {
this.accountbalance = this.accountbalance + amount;
}
回答by Prashant Gurav
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
here your return type is void
and your returning int
.
这里你的返回类型是void
你的返回int
。
回答by ikh
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
public void deposit(double amount) {
return this.accountbalance=this.accountbalance+amount;
}
...Are you trying to return in void
-return function???
...你是想在void
-return 函数中返回吗???
This will be correct:
这将是正确的:
public int setAccountNumber(int accountnumber) {
// ^^^
return accountnumber;
}
public double deposit(double amount) {
// ^^^^^^
return this.accountbalance=this.accountbalance+amount;
}
回答by Ankit Lamba
Forst Error is that the return type of your meyhod is void
and the method returns an int
.
Forst 错误是您void
的方法的返回类型是并且该方法返回一个int
.
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
Change it to :
将其更改为:
public int setAccountNumber(int accountnumber) {
return accountnumber;
}
and the second thing is this is not a good name for a method which is used for getting the variable, change the name of method to :
第二件事是这对于用于获取变量的方法来说不是一个好名字,请将方法名称更改为:
public int getAccountNumber(int accountnumber) { // why is this argument???
return accountnumber;
}
One more thing,your method
还有一件事,你的方法
public void deposit(double amount) {
return this.accountbalance = this.accountbalance + amount;
}
has the same error. It should be :
有同样的错误。它应该是 :
public double deposit(double amount) { //change void to double
return this.accountbalance + amount; //as suggested by SURESH ATTA
}
回答by Brandon
Every answer so far is correct and points out problems. Here is one that is not yet mentioned.
到目前为止,每个答案都是正确的,并指出了问题。这是尚未提及的一项。
Your equals
method does not properly over-ride the parent:
您的equals
方法没有正确覆盖父级:
public boolean equals(BankAccount ba) {
return this.accountbalance == ba.accountbalance;
}
The signature of equals
on Object
is public boolean equals(Object other)
so that any objects can be compared to each other. You have changed it to only allow comparing against other BankAccounts, which violates the contract.
的签名equals
上Object
是public boolean equals(Object other)
这样,任何对象可以相互比较。您已将其更改为仅允许与其他 BankAccounts 进行比较,这违反了合同。
Try this instead:
试试这个:
public boolean equals(BankAccount ba) {
if (ba instanceof BankAccount) {
return this.accountbalance == ((BankAccount) ba).accountbalance;
}
else {
return false;
}
}