ҧ Enum
:: ¹ҡѹ :: Java 2 v 5.0 (Tiger)
หน้า 1 จาก 1
ҧ Enum
package enums;
public class testEnum {
public static void main(String[] args) {
System.out.println(" Apple.GREEN " Apple.GREEN);
Apple apple = Apple.RED;
System.out.println(" " apple);
switch (apple) {
case RED:
System.out.println("ᴧ");
break;
case GREEN:
System.out.println("");
break;
}
System.out.println("----------------------------------");
for(Apple app : Apple.values()){
System.out.println(" " app);
}
}
}
enum Apple{
RED, GREEN, YELLOW
}
Output:Apple.GREEN GREEN
RED
ᴧ
----------------------------------
RED
GREEN
YELLOW
Re: ҧ Enum
enum Apple{
RED, GREEN, YELLOW; // ; Ш
private int price;
}// OK
enum Apple{
private int price; // Error
RED, GREEN, YELLOW;
}// Error
RED, GREEN, YELLOW; // ; Ш
private int price;
}// OK
enum Apple{
private int price; // Error
RED, GREEN, YELLOW;
}// Error
Re: ҧ Enum
enum Apple{
RED(100), GREEN(199), YELLOW(999);
private int price;
Apple(int para){ price = para; } // comment ÷Ѵ Compile Fail
int getPrice(){return price;};
}
¡
System.out.println(" "+Apple.YELLOW.getPrice());
RED(100), GREEN(199), YELLOW(999);
private int price;
Apple(int para){ price = para; } // comment ÷Ѵ Compile Fail
int getPrice(){return price;};
}
¡
System.out.println(" "+Apple.YELLOW.getPrice());
Re: ҧ Enum
enum Apple{
RED(100), GREEN(199), YELLOW(999), BLACK; // BLACK դ()
private int price;
Apple(int para){ price = para; }
Apple(){ } // BLACK Call this Constructor
int getPrice(){return price;};
}
System.out.println(" " Apple.YELLOW.getPrice()); // 999
System.out.println(" " Apple.BLACK.getPrice()); // 0
Re: ҧ Enum
System.out.println(" CompareTo 1 : "+Apple.GREEN.compareTo(Apple.GREEN));
System.out.println(" CompareTo 2 : "+Apple.GREEN.compareTo(Apple.RED));
System.out.println(" CompareTo 3 : "+Apple.GREEN.compareTo(Apple.BLACK));
System.out.println(" CompareTo 4 : "+Apple.RED.compareTo(Apple.BLACK));
CompareTo 1 : 0
CompareTo 2 : 1
CompareTo 3 : -2
CompareTo 4 : -3
System.out.println(" CompareTo 2 : "+Apple.GREEN.compareTo(Apple.RED));
System.out.println(" CompareTo 3 : "+Apple.GREEN.compareTo(Apple.BLACK));
System.out.println(" CompareTo 4 : "+Apple.RED.compareTo(Apple.BLACK));
CompareTo 1 : 0
CompareTo 2 : 1
CompareTo 3 : -2
CompareTo 4 : -3
Similar topics
» ??????ҧ Start ??? Run ??ҧ?ѹ?ѧ???
» ??????ҧ Hello World
» ??????ҧ??????ҧ GUI ???? Swing
» ??????ҧ????? Generic (???????? array ??Ѻ)
» ??????ҧ Hello World
» ??????ҧ??????ҧ GUI ???? Swing
» ??????ҧ????? Generic (???????? array ??Ѻ)
:: ¹ҡѹ :: Java 2 v 5.0 (Tiger)
หน้า 1 จาก 1
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ