COBOL | Java |
---|---|
enum-id Action. 78 #Start. *> Start is a reserved word so use '#' symbol 78 #Stop. 78 #Rewind. 78 #Forward. end enum. enum-id Status. 78 Flunk value 50. 78 Pass value 70. 78 Excel value 90. end enum. class-id MainClass. method-id main static. declare a = type Action::Stop if a not = type Action::Start display a & " is " & a as binary-long *> Prints "Stop is 1" end-if display type Status::Pass as binary-long *> prints 70 display type Status::Pass *> prints "Pass" end method. end class. |
public class enumeration { public enum Action { Start, Stop, Rewind, Forward; } public enum Status { Flunk(50), Pass(70), Excel(90); int mark; public int getMark() { return mark; } Status(int mark) { this.mark = mark; } } public static void main(String[] args) { Action a = Action.Stop; if (a != Action.Start) { // Ordinal is printing out the 0-based index of the entry // rather than its "value" System.out.println(a + " is " + new Integer(a.ordinal()).toString()); System.out.println(Status.Pass.getMark());// can only print value // if enum defines method System.out.println(Status.Pass); // prints Pass } } } |
Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.