luyantl 发表于 2018-9-24 21:51:31

java_枚举

enum Color
{
        red,green,blue;
}

class Enum1
{
        public static void main(String[] args)
        {
                Color c=Color.red;
                //Color c=new Color();        //错误类型:无法实例化枚举对象
                System.out.println(c);

                System.out.println("******************************");
                for(Color c1:Color.values())
                {
                        System.out.println(c1);
                }

                System.out.println(Color.values());                //验证Color.values()是一个数组类型,于是Color.values()就是结果green.
        }
}

luyantl 发表于 2018-9-24 21:58:05

enum Color
{
        red,green,blue;
}

class Enum1
{
        public static void main(String[] args)
        {
                Color c=Color.red;
                //Color c=new Color();        //错误类型:无法实例化枚举对象
                System.out.println(c);

                System.out.println("******************************");
                for(Color c1:Color.values())
                {
                        System.out.println(c1);
                }

                System.out.println(Color.values());                //验证Color.values()是一个数组类型,于是Color.values()就是结果green.
                //用switch语名
                switch(c)
                {
                        case red:
                                System.out.println("用switch语名"+"红色");
                                break;
                        case green:
                                System.out.println("绿色");
                                break;
                }
        }
}

luyantl 发表于 2018-9-24 22:30:09

enum Sex
{
        MALE("男"),FEMALE("女");
        private String title;
        private Sex(String title)
        {
                this.title=title;
        }
        public String toString()
        {
                return this.title;
        }
}
//*************************************
class Person
{
        private String name;
        private int age;
        private Sex sex;
        public Person(String name,int age,Sex sex)
        {
                this.name=name;
                this.age=age;
                this.sex=sex;
        }
        public String toString()
        {
                return "姓名:"+this.name+"年龄:"+this.age+"性别:"+this.sex;
        }
}
//**********************************************************************
public class EnumPerson
{
        public static void main(String[] args)
        {
                Person person=new Person("张三",18,Sex.MALE);
                System.out.println(person.toString());
        }
}

luyantl 发表于 2018-9-24 22:39:26

//去掉枚举类的构造方法
enum Sex
{
        男,女;
        /**
        private String title;
        private Sex(String title)
        {
                this.title=title;
        }
        public String toString()
        {
                return this.title;
        }
        **/
}
//*************************************
class Person
{
        private String name;
        private int age;
        private Sex sex;
        public Person(String name,int age,Sex sex)
        {
                this.name=name;
                this.age=age;
                this.sex=sex;
        }
        public String toString()
        {
                return "姓名:"+this.name+"年龄:"+this.age+"性别:"+this.sex;
        }
}
//**********************************************************************
public class EnumPerson
{
        public static void main(String[] args)
        {
                Sex sex=null;
                Person person=new Person("张三",18,sex.男);
                System.out.println(person.toString());
        }
}
页: [1]
查看完整版本: java_枚举