枚举

package meiju;
public class yunxing {

    public static void main(String[] args)

    {
        System.out.print(Weekday.A);
    }
}

/*class Weekday

{

   public static final Weekday A=new Weekday();

   public static final Weekday B=new Weekday();

   public static final Weekday C=new Weekday();

   public static final Weekday D=new Weekday();

   public static final Weekday E=new Weekday();

   public static final Weekday F=new Weekday();

   public static final Weekday G=new Weekday();

}*/ 

enum Weekday {  A,B,C,D,E; }   //和上面构造的Weekday类功能相同

 

***************************************************************************************

如何定义枚举的构造函数、方法和字段去封装更多的信息

package meiju;

public class yunxing {
    public static void main(String[] args)
    {
        dayin(Weekday.A);
    }
    public static void dayin(Weekday g)//封装打印函数
    {
        String value=g.getWeekday();
        System.out.print(value);
    }

}
//如何定义枚举的构造函数、方法和字段去封装更多的信息
enum
Weekday { A("MOD"),B("TUE"); private String value; private Weekday(String value) //构造函数,必须私有化!因为如果是公有,便可以构造出除了枚举之外的对象 { this.value=value; } public String getWeekday() //获取,因为这个函数在主函数中还要调用,所以必须是public!! { return this.value; } }

&&&&&&&&&&&&&&&&&&&&&上下两个程序相比较
package meiju;
class yunxing
{
   public static void main(String[] args)
   {
       System.out.print(Weekday.C);
   }
}
class Weekday
{
    static String A = "MOD";
    static String B = "TUE";
    static String C = "WEN";
}

 


 **********************************************************************************************************

带抽样方法的枚举

package meiju;

public class yunxing {
    public static void main(String[] args)
    {
        dayin(Weekday.B);
    }
    public static void dayin(Weekday g)//封装打印函数
    {
        String value=g.Chinese();
        System.out.print(value);
    }

}
//带抽样方法的枚举
enum Weekday
{
    A("MOD")
    {
        public  String Chinese()//实现下面定义的抽样方法
        {
            return "星期一";
        }
    }
    ,B("TUE")
    {
        public  String Chinese()
        {
            return "星期二";
        }
    };
    private String value;
    private Weekday(String value)  //把MOD付给A
    {
        this.value=value;
    }
    public String getWeekday()  //获取
    {
        return this.value;
    }
    public abstract String Chinese();//定义抽样方法,定义了之后在枚举的对象里必须实现抽样方法,要不会报错。
   
}

 *******************************************************************************************

自己写个小例子加强理解

package meiju;

public class yunxing
{
    public static void main(String[] args)
    {
        dayin(jieshao.C);
        dayin1(jieshao.C);
    }
    public static void dayin(jieshao a)
    {
        String name=a.getname();
        System.out.println(name);
    }
    public static void dayin1(jieshao a)
    {
        String nianling=a.getage();
        System.out.println("我今年"+nianling+"岁了");
    }
}
enum jieshao
{
   A("12")
   {
       public String getname()
       {
           return "我叫张三";
       }
   },
   B("13")
   {
       public String getname()
       {
           return "我叫李四";
       }
   },
   C("14")
   {
       public String getname()
       {
           return "我叫王五";
       }
   },
   D("17")
     {
       public String getname()
       {
           return "我叫赵六";
       }
   };
   private String age;
   private jieshao(String age)
   {
       this.age=age;
   }
   public String getage()
   {
       return this.age;
   }
   public abstract String getname();
}

 

posted on 2015-01-24 18:45  曾经的码农  阅读(143)  评论(0编辑  收藏  举报