package enumPkg;
public enum CardTypes {//Each enum is objectDEBIT(10,20),CREDIT(0,10),CRYPTO(5,30);
//Object propertiesint fees;int bonus;
//Initilize object using constructorCardTypes(int fee, int bonus){this.fees = fee;this.bonus = bonus;}
//access object propertypublic int getFees(){return this.fees;}
public int getBonus(){return this.bonus;}
}
现在访问其他类中的枚举。在Java中遵循以下过程:
package enumPkg;
public class EnumClass {public static void main(String[] args) {CardTypes cardType = CardTypes.CREDIT; //Each enum element is public static final, when accessed returns a objectSystem.out.println(cardType);
System.out.println("Debit card fees : "+CardTypes.DEBIT.getFees());System.out.println("Debit card bonus : "+CardTypes.DEBIT.getBonus());
CardTypes[] cardTypes = CardTypes.values();//return array of CardTypes i.e all enum elements we have defined
for (CardTypes type : CardTypes.values()) {System.out.println(type); //particular enum objectSystem.out.println(type.ordinal()); //return enum positionSystem.out.println("Bonus : "+type.getBonus()); //return enum object property: BonusSystem.out.println("Fees : "+type.getFees());//return enum object property: Fees}
}}