【问题标题】:The operator > is undefined for the argument type(s) Salary, int运算符 > 未定义参数类型 Salary, int
【发布时间】:2018-04-10 22:16:33
【问题描述】:
public class Employee {

private int id = 0;
private String forename = null;
private String surname = null;
private Salary salary = null;
private CompanyPosition companyposition = null;

public Employee(){

}

public int getId() {
    return id;
}

public String getForename() {
    return forename;
}

public String getSurname() {
    return surname;
}

public Salary getSalary() {
    return salary;
}

public String getPositionName() {
    return getPositionName();
}

public boolean eligibleForBonus(){

    boolean eligibleForBonus;

    if (salary > 40000) {

        eligibleForBonus = true;
    }

    else {
        eligibleForBonus = false;

    }

    return eligibleForBonus;
}

}

对于 if 语句,它说运算符 > 未定义参数类型 Salary, int。我是 Java 新手,所以我不太确定在这里做什么。 Salary 也是一个不同的类,salary 是该类中的一个变量,它是 double 类型。

【问题讨论】:

  • 您将salary 声明为Salary 类的对象-您如何将其与int 进行比较?
  • 您必须从 Salary 类中取出 salary 变量并进行比较。
  • 从逻辑上思考这个问题:> 是一个数学运算符,只能用于 int、double、long、byte 等数值变量。 Salary 是您创建的类,它不是 数字类型(并且不能成为数字类型),因此> 没有任何意义。也许您想比较 Salary 字段之一的值——很难知道,因为我们没有看到 Salary 的代码。

标签: java types arguments undefined operator-keyword


【解决方案1】:

您需要与Salary 类中的变量值进行比较。假设另一个类中的双变量名为salary

if (salary.salary > 40000) {
    ...

或者如果实际工资金额有一个getter calle getAmount 什么的:

if (salary.getAmount()) {
    ...

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2014-06-02
    相关资源
    最近更新 更多