[1725989] | 1 | import java.io.*; |
---|
| 2 | |
---|
| 3 | public class test { |
---|
| 4 | enum Week { |
---|
| 5 | Mon, Tue, Wed, Thu, Fri, Sat, Sun; |
---|
| 6 | public boolean isWeekday() { return ordinal() <= Fri.ordinal(); } |
---|
| 7 | public boolean isWeekend() { return Fri.ordinal() < ordinal(); } |
---|
| 8 | } |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | // object oriented definition |
---|
| 12 | enum Week2 { |
---|
| 13 | // Mon, Tue, Wed, Thu, Fri, Sat, Sun; |
---|
| 14 | Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7); // must appear first |
---|
| 15 | public long day; |
---|
| 16 | private Week2( long d ) { day = d; } |
---|
| 17 | |
---|
| 18 | public boolean isWeek() { |
---|
| 19 | return ordinal() <= Week2.Fri.ordinal(); |
---|
| 20 | } |
---|
| 21 | public boolean isWeekend() { |
---|
| 22 | return Week2.Fri.ordinal() < ordinal(); |
---|
| 23 | } |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | public static void main( String[] args ) { |
---|
| 27 | // non-object oriented usage |
---|
| 28 | Week day = Week.Sat; |
---|
| 29 | |
---|
| 30 | day = Week.valueOf( "Sat" ); |
---|
| 31 | System.out.println( day.ordinal() + " " + day + " " + day.name() ); // 5 Sat |
---|
| 32 | System.out.println( day.isWeekday() ); // false |
---|
| 33 | System.out.println( day.isWeekend() ); // true |
---|
| 34 | |
---|
| 35 | if ( day == Week.Fri ) |
---|
| 36 | System.out.println( "weekday" ); |
---|
| 37 | switch ( day ) { |
---|
| 38 | case Mon: case Tue: case Wed: case Thu: case Fri: |
---|
| 39 | System.out.println( "weekday" ); |
---|
| 40 | break; |
---|
| 41 | case Sat: case Sun: |
---|
| 42 | System.out.println( "weekend" ); |
---|
| 43 | break; |
---|
| 44 | } |
---|
| 45 | for ( Week d : Week.values() ) { |
---|
| 46 | System.out.print( d + " " + d.ordinal() + " " ); |
---|
| 47 | } |
---|
| 48 | System.out.println(); System.out.println(); |
---|
| 49 | |
---|
| 50 | // object oriented usage |
---|
| 51 | Week2 cday = Week2.Sat; |
---|
| 52 | cday.day += 42; |
---|
| 53 | System.out.println( cday.ordinal() + " " + cday.day + " " + cday.name() ); |
---|
| 54 | |
---|
| 55 | System.out.println( cday.isWeek() ); // false |
---|
| 56 | System.out.println( cday.isWeekend() ); // true |
---|
| 57 | |
---|
| 58 | switch ( cday ) { |
---|
| 59 | case Mon: case Tue: case Wed: case Thu: case Fri: |
---|
| 60 | System.out.println( "weekday" ); |
---|
| 61 | break; |
---|
| 62 | case Sat: case Sun: |
---|
| 63 | System.out.println( "weekend" ); |
---|
| 64 | break; |
---|
| 65 | } |
---|
| 66 | for ( Week2 icday : Week2.values() ) { |
---|
| 67 | System.out.print( icday.ordinal() + " " + icday.day + " " + icday.name() + ", " ); |
---|
| 68 | } |
---|
| 69 | System.out.println(); |
---|
| 70 | |
---|
| 71 | int intArray[]; |
---|
| 72 | intArray = new int[20]; |
---|
| 73 | intArray[Week.Fri] = 3; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // java test |
---|