Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enumerated types(JAVA) JDK 1.5 >. إذا غامرت في شرف مروم.... فلا تقنع بمـــا دون النــجـــــــــوم فطعم الموت في أمر حقير.... كطعم الموت في أمر عظيـــــــم.

Similar presentations


Presentation on theme: "Enumerated types(JAVA) JDK 1.5 >. إذا غامرت في شرف مروم.... فلا تقنع بمـــا دون النــجـــــــــوم فطعم الموت في أمر حقير.... كطعم الموت في أمر عظيـــــــم."— Presentation transcript:

1 Enumerated types(JAVA) JDK 1.5 >

2 إذا غامرت في شرف مروم.... فلا تقنع بمـــا دون النــجـــــــــوم فطعم الموت في أمر حقير.... كطعم الموت في أمر عظيـــــــم يرى الجبناء أن العجز عقل.... وتلك خديعة الطبع اللــــئـــيــــــم وكل شجاعة في المرء تغني.... ولا مثل الشجاعة في الحكيم وكم من عائب قولا صحيحا.... وآفته من الفهم الســـقـــيـــــم ولكن تأخذ الآذان مـــــــــنه.... على قــــدر القرائح والعلــــــــوم

3 Enum's  An enumerated type is a data type consisting of a set of named constants called enumerators.  Example :  C++ code  enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

4 java enums are classes !!  C++ enumerations are primitive types and support conversion to and comparison with other integer types. Java enumerations are actually instances of a class (they extend java.lang.Enum ) and... Java Example enum Cardsuit { Clubs, Diamonds, Spades, Hearts };... Cardsuit trump ;

5 You're Kidding Me, Right? It looks like c/c++ code !!

6 java enums are classes what's the meaning ??

7 Ans  may therefore define constructors, fields, and methods as any other class.

8 import java.util.*; enum OperatingSystems { windows, unix, linux, macintosh } public class EnumExample1 { public static void main(String args[]) { OperatingSystems os; os = OperatingSystems.windows; switch(os) { case windows: System.out.println("You chose Windows!"); break; case unix: System.out.println("You chose Unix!"); break; case linux: System.out.println("You chose Linux!"); break; case macintosh: System.out.println("You chose Macintosh!"); break; default: System.out.println("I don't know your OS."); break; } } }

9  Constructor, fields and methods enum OperatingSystems { windows, unix, linux, macintosh; public String v; void setVDst(String Dst){ this.v=Dst; } String setVDst(){ return this.v; } OperatingSystems(){ }

10 //using loop enum OperatingSystems { windows, unix, linux, macintosh } public class NewClass { public static void main(String args[]) { OperatingSystems x=OperatingSystems.windows; for(OperatingSystems o:x.values()){ System.out.println(o); } } }

11 You can give each enum constant a different behavior public enum Operation { PLUS, MINUS, TIMES, DIVIDE; // Do arithmetic op represented by this constant double eval(double x, double y){ switch(this) { case PLUS: return x + y; case MINUS: return x - y; case TIMES: return x * y; case DIVIDE: return x / y; } throw new AssertionError("Unknown op: " + this); }

12 another way You can declare the method abstract in the enum type and override it with a concrete method in each constant.

13 example public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; // Do arithmetic op represented by this constant abstract double eval(double x, double y); } public static void main(String args[]) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); for (Operation op : Operation.values())‏ System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y)); } output $ java Operation 4 2 4.000000 PLUS 2.000000 = 6.000000 4.000000 MINUS 2.000000 = 2.000000 4.000000 TIMES 2.000000 = 8.000000 4.000000 DIVIDE 2.000000 = 2.000000


Download ppt "Enumerated types(JAVA) JDK 1.5 >. إذا غامرت في شرف مروم.... فلا تقنع بمـــا دون النــجـــــــــوم فطعم الموت في أمر حقير.... كطعم الموت في أمر عظيـــــــم."

Similar presentations


Ads by Google