Reputation: 65
I was asked to write a class called BusArrival
, while BusArrival
has only one private instance - Time1 _arrivalTime
. (which is a class wrote, but it's not what my question is about).
Then I needed to call for a constructor in BusArrival
.
private Time1 _arrivalTime;
public BusArrival(int h, int m ,int s) {
if (h < 23 && h > 0)
h = h;
else
h = DEFAULT_HOUR;
if (m < 60 && m > 0)
m = m;
else
m = DEFAULT_MINUTE;
if (s < 60 && s > 0)
s = s;
else
s = DEFAULT_SECOND;
}
but when I do that h, m and s are always returning as 0. if I do the same using Time1 h, s and m it's working fine.
How do I use variables in constructors that I don't already have defined?
Upvotes: 1
Views: 79
Reputation: 7118
If you would like to preserve h, m, s for the object you create declare as private members too, and instead of writing
h = h;
write
this.h = h;
add the following declarations:
private int h, m, s;
Upvotes: 4