鱼C论坛

 找回密码
 立即注册
查看: 3090|回复: 3

[学习笔记] java_枚举

[复制链接]
发表于 2018-9-24 21:51:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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()[1]);                //验证Color.values()是一个数组类型,于是Color.values()[1]就是结果green.
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 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()[1]);                //验证Color.values()是一个数组类型,于是Color.values()[1]就是结果green.
                //用switch语名
                switch(c)
                {
                        case red:
                                System.out.println("用switch语名"+"红色");
                                break;
                        case green:
                                System.out.println("绿色");
                                break;
                }
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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());
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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());
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 11:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表