Would you like to react to this message? Create an account in a few clicks or log in to continue.

ҧ Enum

Go down

ҧ Enum Empty ҧ Enum

ตั้งหัวข้อ  Admin Thu May 22, 2008 3:36 am



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


Admin
Admin
Admin

จำนวนข้อความ : 112
Registration date : 18/05/2008

http://chawban.chocoforum.com

ขึ้นไปข้างบน Go down

ҧ Enum Empty Re: ҧ Enum

ตั้งหัวข้อ  Admin Thu May 22, 2008 3:41 am

enum Apple{
RED, GREEN, YELLOW; // ; Ш
private int price;
}// OK

enum Apple{
private int price; // Error
RED, GREEN, YELLOW;
}// Error
Admin
Admin
Admin

จำนวนข้อความ : 112
Registration date : 18/05/2008

http://chawban.chocoforum.com

ขึ้นไปข้างบน Go down

ҧ Enum Empty Re: ҧ Enum

ตั้งหัวข้อ  Admin Thu May 22, 2008 3:47 am

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());
Admin
Admin
Admin

จำนวนข้อความ : 112
Registration date : 18/05/2008

http://chawban.chocoforum.com

ขึ้นไปข้างบน Go down

ҧ Enum Empty Re: ҧ Enum

ตั้งหัวข้อ  Admin Thu May 22, 2008 3:55 am


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
Admin
Admin
Admin

จำนวนข้อความ : 112
Registration date : 18/05/2008

http://chawban.chocoforum.com

ขึ้นไปข้างบน Go down

ҧ Enum Empty Re: ҧ Enum

ตั้งหัวข้อ  Admin Thu May 22, 2008 4:22 am

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
Admin
Admin
Admin

จำนวนข้อความ : 112
Registration date : 18/05/2008

http://chawban.chocoforum.com

ขึ้นไปข้างบน Go down

ขึ้นไปข้างบน

- Similar topics

 
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ