找不到变量错误?

Cannot find variable error?

所以我想弄清楚为什么当我尝试编辑布尔 equals 方法使其具有 this.dollars == o.dollars 时,它在第二个美元变量上给我一个错误代码代码并说找不到变量?我知道除了这个错误我还有很多其他错误。

此外,对于任何愿意回答的人,那里的 getMoney 和 setMoney 方法有什么用?为什么我需要它而不是美元和美分?

public class Money
{   
private int dollars;
private int cents;
private double money;

public Money (int dol) {
    this.dollars = dol;
    cents = 0;
    if (dol < 0) {
        throw new IllegalArgumentException ("Must be greater than 0.");
    }
}

public Money (int dol, int cent) {
    dol = this.dollars;
    cent = this.cents;
    if (dol < 0 || cent < 0) {
        throw new IllegalArgumentException ("Must be greater than 0.");
    }
}

public Money (Money other) {
    this.dollars = other.dollars;
    this.cents = other.cents;
    this.money = other.money;
}

public int getDollars () {
    return dollars;
}

public int getCents () {
    return cents;
}

public void setMoney (int dollars, int cents) {
    dollars = this.dollars;
    cents = this.cents;
    if (dollars < 0 || cents < 0) {
        throw new IllegalArgumentException ("Must be greater than 0.");
    }
    if (cents > 100) {
        int c = cents/100;
        int m = dollars + c;
    }
}

public double getMoney () {
    return money;
}

public void add (int dollars) {
    if (dollars < 0) {
        throw new IllegalArgumentException ("Must be greater than 0.");
    }
}

public void add (int dollars, int cents) {
    if (dollars < 0 || cents < 0) {
        throw new IllegalArgumentException ("Must be greater than 0.");
    }
}

public void add (Money other) {
}

public boolean equals (Object o) {

}

public String toString () {
    String c = String.format("%.02d",cents);
    return "$" + dollars + "." + c;
}

}

这是错误的方法

public Money (int dol, int cent) {
    dol = this.dollars;
    cent = this.cents;
    if (dol < 0 || cent < 0) {
        throw new IllegalArgumentException ("Must be greater than 0.");
    }
}

尝试

public Money (int dol, int cent) {
    this.dollars = dol;
    this.cents = cent;
    if (dol < 0 || cent < 0) {
        throw new IllegalArgumentException ("Must be greater than 0.");
    }
}

同样适用于 public void setMoney (int dollars, int cents) {

关于 equals

equals 中,应使用 instanceof 测试 o 对象以查看它是否是 Money 对象,如果是,则可以将其转换为货币对象

if( o instanceof Money) {
   return this.dollars == ((Money)o).dollars && this.cents == ((Money)o).cents;  // etc
}