You may also wish to use getters and setters to provide access to
imaginary instance variables that exist only in the sense that their
values can be computed from instance variables that do exist. For example,
you can create getHours
and setHours
, which seem to refer to
the contents of an imaginary hours
instance variable, of type
double
, but which actually work with the contents of the
minutes
instance variable:
public class Attraction { public int minutes; public Attraction () {minutes = 75;} public Attraction (int m) {minutes = m;} // Define getters: public int getMinutes () {return minutes;} public double getHours () { return minutes / 60.0; } // Define setters: public void setMinutes (int m) {minutes = m;} public void setHours (double h) { minutes = (int)(h * 60); } }