I have a few classes.
class Money {
protected amount: number
public equals(object: Object): boolean {
const money: Money = object as Money;
return this.amount === **money.amount** //<- I want to write like this
}
}
class Dollar extends Money {
constructor(amount: number){
super();
this.amount = amount;
}
}
and here is how you use it:
new Dollar(5).equals(new Dollars(5));
However, I am unable to access the `money.amount` in the Money class.
This prompted me to try the following solution:
class Money {
protected amount: number;
getAmount(): number {
return this.amount;
}
public equals(object: Object): boolean {
const money: Money = object as Money;
return this.amount === money.getAmount(); // tried
}
}
Alternatively, I considered removing the `protected` keyword within the Money class.
The solution works, but I don't find it ideal.
How can I address this issue similar to Java?