Several constructors may be used for convenience:
Each constructs an object out of different types of arguments.
No two constuctors may have exactly the same types of arguments (otherwise the compiler could not tell which constructor is meant).
The most obvious constructor constructs a Quantity out of a multiplier, numerator, and denominator:
class Quantity{double multiplier;StringList numerator;StringList denominator;
Quantity(double multiplier, // constructor headStringList numerator,StringList denominator){ // constructor bodythis.multiplier = multiplier;this.numerator = numerator;this.denominator = denominator;}
.... other constructors and methods for Quantity ....}
The keyword this refers to the object being constructed.
The argument variable names over-ride the names in the object declaration.
Next Slide | Previous Slide | Contents |