2013-10-18 54 views
-2

如何获取eligibleForBonus()的输出以用于下面的toString方法。我已经使用“this.isBonus”作为占位符,但它不起作用,因为isBonus不是班级中的变量。toString java?我怎样才能得到在toString中使用的方法的返回?

public String eligibleForBonus(double salary){ 

    String isBonus; 

    if (salary >= 40000) { 
     isBonus = "is"; 
    } 
    else { 
     isBonus = "is not"; 
    } 
    return isBonus; 
    } 

    } 

@Override 
public String toString() { 
     return this.forename + " " + this.surname + " (" + this.id + "): " + this.companyPosition + " at " + this.salary + " and " + this.isBonus + " eligible for bonus."; 

} 
+4

调用方法? –

+3

我宁愿让'isBonus'为'boolean'类型。 –

+0

@RohitJain我曾经看到有两个字段的代码:'hasSomeProperty'和'doesNotHaveSomeProperty'作为相同属性的布尔值。 –

回答

3

随着罗希特说,只要调用方法来获得回报:

return this.forename + " " + this.surname + " (" + this.id + "): " + this.companyPosition + " at " + this.salary + " and " + this.eligibleForBonus(this.salary) + " eligible for bonus."; 

一对夫妇的意见:

  1. 为罗希特表明,返回一个布尔(再利用三元运算符生成文本)更清洁。
  2. 如果班级有工资作为财产,为什么你必须将其转化为方法?