Java by API/java.lang/Enum — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

Enum: compareTo(Object o)

/*
A comes before B
 */
enum Apple {  
  A, B, C, D, E 
} 
 
public class MainClass { 
  public static void main(String args[])  
  { 
    Apple ap, ap2, ap3; 
    
    ap =  Apple.A; 
    ap2 = Apple.B; 
    ap3 = Apple.C; 
    if(ap.rupareTo(ap2) < 0) 
      System.out.println(ap + " comes before " + ap2); 
 
    if(ap.rupareTo(ap2) > 0) 
      System.out.println(ap2 + " comes before " + ap); 
 
    if(ap.rupareTo(ap3) == 0) 
      System.out.println(ap + " equals " + ap3); 
   
    System.out.println(); 
 
    if(ap.equals(ap2)) 
      System.out.println("Error!"); 
 
    if(ap.equals(ap3)) 
      System.out.println(ap + " equals " + ap3); 
 
    if(ap == ap3) 
      System.out.println(ap + " == " + ap3); 
  } 
}





Enum: equals(Object other)

/*
A comes before B
 */
enum Apple {  
  A, B, C, D, E 
} 
 
public class MainClass { 
  public static void main(String args[])  
  { 
    Apple ap, ap2, ap3; 
    
    ap =  Apple.A; 
    ap2 = Apple.B; 
    ap3 = Apple.C; 
    if(ap.rupareTo(ap2) < 0) 
      System.out.println(ap + " comes before " + ap2); 
 
    if(ap.rupareTo(ap2) > 0) 
      System.out.println(ap2 + " comes before " + ap); 
 
    if(ap.rupareTo(ap3) == 0) 
      System.out.println(ap + " equals " + ap3); 
   
    System.out.println(); 
 
    if(ap.equals(ap2)) 
      System.out.println("Error!"); 
 
    if(ap.equals(ap3)) 
      System.out.println(ap + " equals " + ap3); 
 
    if(ap == ap3) 
      System.out.println(ap + " == " + ap3); 
  } 
}





Enum: name()

enum ProgramFlags {
    showErrors(0x01),
    includeFileOutput(0x02),
    useAlternateProcessor(0x04);
    private int bit;
    ProgramFlags(int bitNumber)
    {
        bit = bitNumber;
    }
    public int getBitNumber()
    {
        return(bit);
    }
}
public class EnumBitmapExample {
    public static void main(String args[])
    {
        ProgramFlags flag = ProgramFlags.showErrors;
        System.out.println("Flag selected is: " +
                                flag.ordinal() +
                           " which is " +
                                flag.name());
    }
}





Enum: ordinal()

/*
A 0
B 1
C 2
D 3
E 4
 */
enum Apple {  
  A, B, C, D, E 
} 
 
public class MainClass { 
  public static void main(String args[])  
  { 
    for(Apple a : Apple.values()) 
      System.out.println(a + " " + a.ordinal()); 
 
  } 
}





Enum: valueOf(String v)

/*
Here are all Apple constants
A
B
C
D
E
ap contains C
 */
enum Apple {  
  A, B, C, D, E 
} 
 
public class MainClass { 
  public static void main(String args[])  
  { 
    Apple ap; 
 
    System.out.println("Here are all Apple constants"); 
    Apple allapples[] = Apple.values(); 
    for(Apple a : allapples) 
      System.out.println(a); 
 
    System.out.println(); 
    
    ap = Apple.valueOf("C"); 
    System.out.println("ap contains " + ap); 
 
  } 
}





Enum: values()

/*
Here are all Apple constants
A
B
C
D
E
ap contains C
 */
enum Apple {  
  A, B, C, D, E 
} 
 
public class MainClass { 
  public static void main(String args[])  
  { 
    Apple ap; 
 
    System.out.println("Here are all Apple constants"); 
    Apple allapples[] = Apple.values(); 
    for(Apple a : allapples) 
      System.out.println(a); 
 
    System.out.println(); 
    
    ap = Apple.valueOf("C"); 
    System.out.println("ap contains " + ap); 
 
  } 
}