m_naish

enum examples

Nov 20th, 2017
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. usging enum to declare elements:
  2. ================================
  3. public enum Elements
  4. {
  5.     Fire, Water, Air, Earth;
  6. }
  7.  
  8. creating instances of enums:
  9. ============================
  10. public enum Instances
  11.  {
  12.     //One instantiated with empty constructor (enum are Great)
  13.     One,
  14.     //Two instantiated with overloaded constructor with String parameter(Zeev)
  15.     Two("Zeev");
  16.     //enum can store use properties
  17.     private String text;
  18.     //enum can use private constructor
  19.     private Instances(){
  20.         this.text="enum are Great";
  21.     }
  22.     //enum can use overloading...
  23.     private Instances(String text)
  24. {
  25.         this.text=text;
  26.     }
  27.     //enum can use methods
  28.     public String getText()
  29.     {
  30.         return this.text;
  31.     }
  32.     public void setText(String text)
  33.     {
  34.         this.text=text;
  35.     }
  36.    
  37. }
  38.  
  39. using interface with enum
  40. ==========================
  41. public interface Thirdable
  42. {
  43.     public void printIt(String it);
  44. }
  45.  
  46. //forcing Third enum to implement specific interface
  47. public enum Third implements Thirdable
  48. {
  49.     zeev;//Third instance
  50.  
  51.     //Default constructor use inherited .printIt() method
  52.     private Third() {
  53.         this.printIt("created");
  54.     }
  55.     @Override
  56.     public void printIt(String it)
  57.     {
  58.         System.out.println("Third prints: "+it);
  59.     }
  60. }
  61.  
  62.  
  63.  
  64. using the enum (tester)
  65. ======================
  66. public class Testerit
  67. {
  68.     public static void main(String[] args) {
  69.  
  70.         Elements elem;
  71.             elem = Elements.Fire;
  72.             System.out.println(elem==Elements.Water);//
  73.             System.out.println(elem==Elements.Fire);//
  74.             //
  75.         switch (elem){
  76.             case Fire:
  77.                 System.out.println("Taase A");
  78.             break;
  79.             case Water:
  80.                 System.out.println("Taase B");
  81.             break;
  82.             case Earth:
  83.             case Air:
  84.                 System.out.println("Taase C");
  85.         }
  86.  
  87.         //possible -> elem it's local variable of Elements type
  88.         elem = Elements.Air;
  89.         //impossible -> elem.Air is constant which cannot be changed
  90.         //elem.Air=Elements.Fire;
  91.        
  92.         // .values() method -> returns Array of enum instances
  93.         Elements [] elems=Elements.values();
  94.         //change elem variable to first element
  95.         elem=elems[0];
  96.         // .name() method -> returns String representation for enum's instance Name
  97.         String elemName=elem.name();
  98.         System.out.println("we got "+elemName);
  99.  
  100.        
  101.         Instances inst = Instances.One;
  102.         System.out.println(inst.getText());
  103.         inst.setText("I am Groot");
  104.         System.out.println(inst.getText());
  105.        
  106.         System.out.println(Instances.Two.getText());
  107.        
  108.         inst = null;
  109.        
  110.         System.out.println("here:"+Instances.One.values()[1].values()[0].getText());
  111.         Third.zeev.printIt("Enum is Awesome");
  112.        
  113.     }
  114.    
  115. }
Add Comment
Please, Sign In to add comment