方法1:
1、Fruit.java

1 /** 2 * 水果的枚举类 3 * @author Administrator 4 * 5 */ 6 public enum Fruit { 7 //apple调用有两个参数的构造,构造可以跟匿名内部类 8 apple(6,3){ 9 public String fname(){ 10 return "苹果"; 11 } 12 }, 13 orange{ 14 public String fname(){ 15 return "橘子"; 16 } 17 }, 18 peat{ 19 public String fname(){ 20 return "梨"; 21 } 22 }, 23 banana{ 24 public String fname(){ 25 return "香蕉"; 26 } 27 }; 28 private int num;//个数 29 private int price;//价格 30 31 32 private Fruit() { 33 } 34 private Fruit(int num , int price) { 35 this.num = num; 36 this.price = price; 37 } 38 39 //抽象方法,返回水果的名字 40 public abstract String fname(); 41 42 public int getNum() { 43 return num; 44 } 45 46 public void setNum(int num) { 47 this.num = num; 48 } 49 50 public int getPrice() { 51 return price; 52 } 53 54 public void setPrice(int price) { 55 this.price = price; 56 } 57 58 }
2、FruitTest.java

1 public class FruitTest { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 Fruit apple = Fruit.apple; 8 System.out.println("有"+apple.getNum()+ 9 "个"+apple.fname()+ 10 "共"+apple.getPrice()+"元"); 11 } 12 13 }
方法2:
1、Week1.java

1 public class Week1 { 2 public static final int MON = 1; 3 public static final int TUS = 2; 4 public static final int WED = 3; 5 public static final int THU = 4; 6 public static final int FIR = 5; 7 public static final int SAT = 6; 8 public static final int SUN = 7; 9 }
2、Test.java

1 public class Test { 2 public static void main(String[] args) { 3 Week2 day = Week2.MON; 4 Test test = new Test(); 5 test.week(day); 6 } 7 8 public void week(Week2 day){ 9 switch (day) { 10 case MON: 11 System.out.println("星期一,上课"); 12 break; 13 14 default: 15 System.out.println("输入错误!"); 16 break; 17 } 18 } 19 }
方法3:
1、Week2.java

1 public enum Week2 { 2 //每个枚举常量都相当于一个对象 3 MON,TUS,WED,THU,FIR,SAT,SUN 4 }
2、

1 public class Test2 { 2 public enum Fruite{ 3 apple(6,3),orange(4,2),banana(5,4),peat(7,5); 4 5 private Fruite() { 6 } 7 8 private Fruite(int price , int num) { 9 this.price = price; 10 this.num = num; 11 12 } 13 14 private int price; 15 private int num; 16 17 public int getNum() { 18 return num; 19 } 20 21 public void setNum(int num) { 22 this.num = num; 23 } 24 25 public int getPrice() { 26 return price; 27 } 28 29 public void setPrice(int price) { 30 this.price = price; 31 } 32 } 33 public static void main(String[] args) { 34 Fruite apple = Fruite.apple; 35 System.out.println("苹果的价格:"+apple.price+"苹果的数量:"+apple.num); 36 } 37 }