小工具知识网

当前位置: 首页 > 行业知识 > java枚举

行业知识

java枚举

2026-12-04 22:20:43 来源:互联网转载

目录

()

1.枚举的简介

2.枚举的形式和常用方法

()

3.枚举中的构造方法

4.练习实例


1.枚举的简介

枚举就是相当于一种来存放常量的特殊的类,是一种新的常量定义方式,提供了安全检查功能,简而言之就是把常量搞的有对象的特性了,类型也是和类差不多所以方法调用枚举类型的常量时其他类型的常量无法没方法调用。下面例子有。

使用枚举的优点:

  1. 类型安全
  2. 紧凑有效的数据定义
  3. 可以和程序其他部分完美交互
  4. 运行效率高

2.枚举的形式和常用方法

主要:

  1. 可以把枚举看成一个类,继承于java.lang.Enum
  2. 枚举的成员就可以看做是一个个实例
  3. 枚举成员的类型默认为,final和public和static修饰

枚举的常用方法

枚举的常用方法
方法名称含义
values()将枚举成员以数组形式还回
valueOf()将字符串转化成枚举类型
compareTo()必将两个枚举对象定义的顺序
ordinal()还回枚举成员的索引

3.枚举中的构造方法

注意:

  1. 枚举中的构造方法必须用private修饰,所以默认为private,可以省略
  2. 构造方法基本使用于有参枚举成员,再通过创建get方法获得参数
  3. 具体看实例

4.练习实例

枚举常见用法实例:

package com.c.cn;//常量设置时通常放在接口中interface Constants{    public static final int Constant_A=1;    public static final int Constant_B=2;}public class EnumClass{    //enum为关键字,在枚举中定义常量,枚举可以放在类里面    enum Constant1{       Constant_A ,Constant_B,Constant_C ,Constant_D    }  //使用接口定义的常量的方法    public static void intf(int c){        switch (c){            case Constants.Constant_A:                System.out.println("我是接口中的常量"+Constants.Constant_A);                break;            case Constants.Constant_B:                System.out.println("我是接口中的常量"+Constants.Constant_B);                break;        }    }    //compareTo()方法的使用    public static void compare(Constant1 constant1){        for (Constant1 c:Constant1.values()             ) {            System.out.println(constant1+"与"+c+"的比较结果为;"+constant1.compareTo(c));        }    }    //使用枚举定义的常量的方法    public static void enu(Constant1 c){        switch (c){            case Constant_A:                System.out.println("我是枚举中的常量"+Constant1.Constant_A);                break;            case Constant_B:                System.out.println("我是枚举中的常量"+Constant1.Constant_B);                break;        }    }    public static void main(String[] args) {           EnumClass.intf(Constants.Constant_A);           EnumClass.intf(Constants.Constant_B);           EnumClass.enu(Constant1.Constant_A);           EnumClass.enu(Constant1.Constant_B);           EnumClass.intf(2);        // EnumClass.enu(2);这个会报错因为这个方法只接受枚举类型的参数        //空两行        System.out.println();        //方法values()的使用        for (Constant1 c:Constant1.values()             ) {            System.out.println("枚举的成员变量:"+c);        }        System.out.println();        //valueOf()方法的使用        compare(Constant1.valueOf("Constant_B"));        System.out.println();        //ordinal()方法的使用        for (Constant1 c1:Constant1.values()             ) {            System.out.println(c1+"的索引值为:"+c1.ordinal());        }        }    }

结果:

我是接口中的常量1

我是接口中的常量2

我是枚举中的常量Constant_A

我是枚举中的常量Constant_B

我是接口中的常量2

枚举的成员变量:Constant_A

枚举的成员变量:Constant_B

枚举的成员变量:Constant_C

枚举的成员变量:Constant_D

Constant_B与Constant_A的比较结果为;1

Constant_B与Constant_B的比较结果为;0

Constant_B与Constant_C的比较结果为;-1

Constant_B与Constant_D的比较结果为;-2

Constant_A的索引值为:0

Constant_B的索引值为:1

Constant_C的索引值为:2

Constant_D的索引值为:3

 

枚举构造方法实例:

package com.c.cn;public class EnumClass1 {    enum Book{        LANGUAGE("语文"),        MATH("数学"),        ENGLISH("英语");        //枚举中的构造方法必须是private修饰        private String string;        Book(){//定义无参构造方法        }        //定义有参构造方法,枚举中的构造方法默认为private        Book(String s){            this.string="我是"+s+"书";        }        //获取string的值      public String getString(){            return string;        }    }    public static void main(String[] args) {        for (Book b:Book.values()             ) {            System.out.println(b.getString());        }    }}

结果:

我是语文书

我是数学书

我是英语书

 

上一篇:非旅迅雷下载秘籍,一键掌握高效下载技巧!

下一篇:hcml怎么进服务器?