Re: [Asking] TestEnum2.java
Quote:
Originally Posted by
expert_developer
My question :
1. In this line
Code:
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY {
public int workTime() {
return 5;
}
};
I think the value return to 5, but why value of SATURDAY(false) is different with MONDAY(false) ?
Both SATURDAY and MONDAY use the default constructor and thus set the holiDay field to false, but SATURDAY has an override for the workTime() method (returning 5), while MONDAY and all the other weekday enums use the default workTime() method which returns 8.
Quote:
2. How could this line
Code:
public int workTime(){
return 8;
}
effect the value of SATURDAY(false) ?
It doesn't because SATURDAY overrides this method.
Re: [Asking] TestEnum2.java
Quote:
Originally Posted by
Fubarable
Both SATURDAY and MONDAY use the default constructor and thus set the holiDay field to false, but SATURDAY has an override for the workTime() method (returning 5), while MONDAY and all the other weekday enums use the default workTime() method which returns 8.
It doesn't because SATURDAY overrides this method.
Which part of codes that show override ?
:)
Re: [Asking] TestEnum2.java
This is MONDAY's declaration:
This is TUESDAY's declaration:
This is SATURDAY's declaration:
Code:
SATURDAY {
// method override below
public int workTime() {
return 5;
}
};
See the difference?
Re: [Asking] TestEnum2.java
So, the value of MONDAY up to FRIDAY automatically return to 8 ?
And only SATURDAY has value return to 5 ?
Re: [Asking] TestEnum2.java
Yes. Monday -> Friday use the workTime() method at the end of the enum. But Saturday uses the overridden form that returns 5.
Re: [Asking] TestEnum2.java
I see.
I thought that MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY have same value return with
SATURDAY {
public int workTime() {
return 5;
}
};
Re: [Asking] TestEnum2.java
It can be confusing; so you're not alone. Perhaps giving each Days enum its own line would make it less confusing:
Code:
enum Days {
SUNDAY(true) {
public int workTime() {
return 0;
}
},
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY {
public int workTime() {
return 5;
}
};
private boolean holiDay;
Days() {
this.holiDay = false;
}
Days(boolean holiDay) {
this.holiDay = holiDay;
}
public boolean isHoliDay() {
return holiDay;
}
public int workTime() {
return 8;
}
}
Oh and "days" should be capitalized, "Days" since it represents the name of the enums.
Re: [Asking] TestEnum2.java
Oh I see.
Now I understand about it. Thanks for your explanation and suggestion.
I hope you'ld like to teach me more about java programming because I need to learn a lot.
:)