Thursday, November 3, 2016
BÃ i 35 Enum trong java
BÃ i 35 Enum trong java
Bài: 35 Enum trong java
?
Ki?u d? li?u Enum g?n gi?ng v?i ki?u d? li?u Array, nh?ng các ph?n t? có th? b? sung thêm các ph??ng th?c!
Enum là 1 ki?u d? li?u li?t kê. Giúp b?n t? ch?c d? li?u khoa h?c h?n, code ???c trong sáng d? hi?u h?n.
Ví d?: L?y t? blog StudyAndShare:
?
?
PHP:
package javaandroidvn;
public class JavaAndroidVn {
enum COLOR {
RED, BLUE, YELLOW
};
public static void main(String[] args) {
System.out.println(COLOR.BLUE);
System.out.println(COLOR.RED);
System.out.println(COLOR.YELLOW);
System.out.println("Cách 2: ");
COLOR c = COLOR.RED;
System.out.println(c);
System.out.println(c.YELLOW);
if (c == COLOR.RED) {
System.out.println("True");
} else {
System.out.println("False");
}
}
}Code demo:
PHP:
package javaandroidvn;
public class JavaAndroidVn {
enum COLOR {
RED, BLUE, YELLOW
};
public static void main(String[] args) {
COLOR c = COLOR.RED;
switch (c) {
case RED: {
System.out.println("Red");
break;
}
case BLUE: {
System.out.println("Blue");
break;
}
case YELLOW: {
System.out.println("Yellow");
break;
}
}
}
}Ví d?: L?y d?a vào Video blog StudyAndShare:
?
PHP:
package javaandroidvn;
public class JavaAndroidVn {
enum COLOR {
RED(5), BLUE(6), YELLOW(7), GREEN(8);
private int value;
COLOR(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
};
public static void main(String[] args) {
COLOR c = COLOR.BLUE;
System.out.println(c + ": " + c.getValue());
System.out.println("ordinal BLUE: " + c.ordinal());
c = COLOR.GREEN;
System.out.println("ordinal GREEN: " + c.ordinal());
}
}Available link for download