文章目录
传送门:
06 JAVASE异常和常用类
1. 异常
异常是指在程序的运行过程中所发生的不正常的情况,它会中断正在运行的程序。Java编程语言使用异常处理机制为程序提供了错误处理的能力。异常是一种特殊对象,类型为java.lang.Exception
或其子类。
注意:
- 相同的代码在运行的时候,根据输入的参数或者操作的不同,有可能会发生异常,有可能不会发生异常。应该在写代码的过程中尽可能的保证代码的正确性。
- 如果要解决代码中出现的异常,需要添加非常复杂的代码逻辑来判断,会使代码变得非常臃肿,可读性比较差,不利于维护;因此,推荐大家使用异常机制来处理程序运行过程中出现的问题。
- 程序在运行过程中如果出现了问题,会导致后面的代码无法正常执行,而使用异常机制之后,可对异常情况进行处理。同时,后续的代码会继续执行,不会中断整个程序。
1.1 异常处理机制
Java的异常处理是通过5个关键字来实现的:try、catch、finally、throw、throws
注意:当使用多重catch的时候一定要注意相关异常的顺序,将子类放在最前面的catch
package com.mashibing;
import java.util.Scanner;
public class TestException {
public static void main(String[] args) {
try{
Scanner in = new Scanner(System.in);
System.out.print("请输入被除数:");
int num1 = in.nextInt();
System.out.print("请输入除数:");
int num2 = in.nextInt();
System.out.println(String.format("%d / %d = %d",
num1, num2, num1/ num2));
}catch (Exception e) {
System.out.println("出现异常!!!");
}
System.out.println("感谢使用本程序!");
}
}
请输入被除数:10
请输入除数:0
出现异常!!!
感谢使用本程序!
使用try-catch块捕获异常,分为三种情况:
-
第一种情况:正常
public void method(){ try { // 代码段(此处不会产生异常) } catch (异常类型 ex) { // 对异常进行处理的代码段 } // 代码段 }
-
第二种情况:出现异常,先处理try中异常代码之前的逻辑,后面的逻辑不会执行,最后会执行catch中的代码
public void method(){ try { // 代码段 1 // 产生异常的代码段 2 // 代码段 3 } catch (异常类型 ex) { // 对异常进行处理的代码段4 } // 代码段5 }
-
第三种情况:使用多重catch的时候,会遇到异常类型不匹配,此时依然会报错,因此建议在catch的最后将所有异常的父类写上
在异常的处理过程中,不要只是简单的输出错误,要尽可能的将详细的异常信息进行输出。
1.2 常见的异常类型
1.3 多重catch块
引发多种类型的异常
- 排列catch 语句的顺序:先子类后父类
- 发生异常时按顺序逐个匹配
- 只执行第一不异常类型匹配的catch语句
public void method(){
try {
// 代码段
// 产生异常(异常类型2)
} catch (异常类型1 ex) {
// 对异常进行处理的代码段
} catch (异常类型2 ex) {
// 对异常进行处理的代码段
} catch (异常类型3 ex) {
// 对异常进行处理的代码段
}
// 代码段
}
1.4 try-catch-finally
finally块中的代码总是会被执行。
package com.mashibing;
/*
* 在程序运行过程中,如果处理异常的部分包含finally的处理,那么无论代码是否发生异常,finally中的代码总会执行
* finally中包含哪些处理逻辑:
* 1.IO流的关闭操作一般设置在finally中
* 2.数据库的连接关闭操作设置在finally中
* */
public class FinallyDemo {
public static void main(String[] args) {
try {
System.out.println(1/0);
}catch (Exception e) {
e.printStackTrace();
}finally {
System.out.println("我是finally处理块");
}
}
}
java.lang.ArithmeticException: / by zero
at com.mashibing.FinallyDemo.main(FinallyDemo.java:6)
我是finally处理块
面试题:try-catch块中存在return语句,是否还执行finally块,如果执行,说出执行顺序
-
情况一(try中有return,finally中没有return)
public class TryTest{ public static void main(String[] args){ System.out.println(test()); } private static int test(){ int num = 10; try{ System.out.println("try"); return num += 80; }catch(Exception e){ System.out.println("error"); }finally{ if (num > 20){ System.out.println("num>20 : " + num); } System.out.println("finally"); } return num; } }
输出:
try num>20 : 90 finally 90
分析:显然
return num += 80
被拆分成了num = num+80
和return num
两个语句,先执行try中的num = num+80
语句,将其保存起来,在try中的return num
执行前,先将finally中的语句执行完,而后再将90返回。 -
情况二(try和finally中均有return)
public class TryTest{ public static void main(String[] args){ System.out.println(test()); } private static int test(){ int num = 10; try{ System.out.println("try"); return num += 80; }catch(Exception e){ System.out.println("error"); }finally{ if (num > 20){ System.out.println("num>20 : " + num); } System.out.println("finally"); num = 100; return num; } } }
try num>20 : 90 finally 100
分析:try中的return语句同样被拆分了,finally中的return语句先于try中的return语句执行,因而try中的return被”覆盖“掉了,不再执行。
-
情况三(finally中改变返回值num)
public class TryTest{ public static void main(String[] args){ System.out.println(test()); } private static int test(){ int num = 10; try{ System.out.println("try"); return num; }catch(Exception e){ System.out.println("error"); }finally{ if (num > 20){ System.out.println("num>20 : " + num); } System.out.println("finally"); num = 100; } return num; } }
try finally 10
分析:虽然在finally中改变了返回值num,但因为finally中没有return该num的值,因此在执行完finally中的语句后,test()函数会得到try中返回的num的值,而try中的num的值依然是程序进入finally代码块前保留下来的值,因此得到的返回值为10。
-
情况四(将num的值包装在Num类中)
public class TryTest{ public static void main(String[] args){ System.out.println(test().num); } private static Num test(){ Num number = new Num(); try{ System.out.println("try"); return number; }catch(Exception e){ System.out.println("error"); }finally{ if (number.num > 20){ System.out.println("number.num>20 : " + number.num); } System.out.println("finally"); number.num = 100; } return number; } } class Num{ public int num = 10; }
try finally 100
分析:从结果中可以看出,同样是在finally中改变了返回值num的值,在情况三中,并没有被try中的return返回(test()方法得到的不是100),但在这里却被try中的return语句返回了。
总结:
try语句在返回前,将其他所有的操作执行完,保留好要返回的值,而后转入执行finally中的语句,而后分为以下三种情况:
- 情况一:如果finally中有return语句,则会将try中的return语句”覆盖“掉,直接执行finally中的return语句,得到返回值,这样便无法得到try之前保留好的返回值。
- 情况二:如果finally中没有return语句,也没有改变要返回值,则执行完finally中的语句后,会接着执行try中的return语句,返回之前保留的值。
- 情况三:如果finally中没有return语句,但是改变了要返回的值,这里有点类似于引用传递和值传递的区别,分以下两种情况:
- 如果return的数据是基本数据类型或文本字符串,则在finally中对该基本数据的改变不起作用,try中的return语句依然会返回进入finally块之前保留的值。
- 如果return的数据是引用数据类型,而在finally中对该引用数据类型的属性值的改变起作用,try中的return语句返回的就是在finally中改变后的该属性的值。
面试题:try-catch- finally块中,finally块唯一不执行的情况是什么?
除非在try块或者catch块中调用了退出虚拟机的方法System.exit(1);
,否则不管在try块、catch块中执行怎样的代码,出现怎样的情况,异常处理的finally块总是会被执行的。使用System.exit(1)
语句可以退出Java虚拟机,因此不执行finally。
public class TryTest{
public static void main(String[] args){
test();
}
public static void test(){
try{
System.out.println("try");
// int i = 1 / 0;
System.exit(1);
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}finally{
System.out.println("finally");
}
}
}
1.5 上机练习:根据编号输出课程名称
需求说明:
-
按照控制台提示输入1~3之间任一个数字,程序将输出相应的课程名称
-
根据键盘输入进行判断。如果输入正确,输出对应课程名称。 如果输入错误,给出错误提示。
-
不管输入是否正确,均输出“欢迎提出建议”语句
package com.mashibing;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExerciseDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入课程编号:");
try{
int i = scanner.nextInt();
if(i==0){
System.out.println("java");
}else if(i==1){
System.out.println("scala");
}else{
System.out.println("python");
}
}catch (InputMismatchException e){
e.printStackTrace();
}finally {
System.out.println("欢迎选课");
}
}
}
请输入课程编号:1
scala
欢迎选课
1.6 throws声明异常
在异常情况出现的时候,可以使用try-catch-finally
的方式对异常进行处理,除此之外,可以将异常向外抛出,由外部的进行处理。
-
在方法调用过程中,可以存在N多个方法之间的调用,此时假如每个方法中都包含了异常情况。 那么就需要在每个方法中都进行
try-catch-finally
。另外一种比较简单的方式,就是在方法的最外层调用处理一次即可。使用throws的方法,对所有执行过程中的所有方法出现的异常进行统一集中处理 -
如何判断是使用throws还是使用try…catch…。最稳妥的方式是在每个方法中都进行异常的处理。偷懒的方式是判断在整个调用的过程中,外层的调用方法是否有对异常的处理,如果有,直接使用throws,如果没有,那么就要使用try…catch…。
package com.mashibing;
public class ExceptionDemo {
public static void test1() throws Exception{
System.out.println(1/0);
}
public static void test2() throws Exception {
test1();
System.out.println(100/0);
}
public static void test3() throws Exception{
test2();
}
public static void test4() throws Exception{
test3();
}
}
1.7 自定义异常
在java的api中提供了非常丰富的异常类,但是在某些情况下不太满足我们的需求,此时,需要自定义异常。一般情况下不需要,但是在公司要求明确或者要求异常格式规范统一的时候是必须要自己实现的。
步骤:
- 定义异常类继承Exception类
- 编写构造方法,继承父类的实现
- 实例化自定义异常对象
- 使用
throw new 自定义异常的名称
抛出
GenderException.java
:
package com.mashibing;
public class GenderException extends Exception{
public GenderException() {
System.out.println("性别异常");
}
public GenderException(String msg) {
System.out.println(msg);
}
}
ExceptionDemo.java
:
package com.mashibing;
public class ExceptionDemo {
public static void show() throws GenderException{
String gender = "1234";
if (gender.equals("man")){
System.out.println("man");
}else if(gender.equals("woman")){
System.out.println("woman");
}else{
//抛出异常
throw new GenderException("gender is wrong");
}
}
public static void main(String[] args) {
try {
show();
} catch (GenderException e) {
e.printStackTrace();
}
System.out.println("hehe");
}
}
gender is wrong
hehe
com.mashibing.GenderException
at com.mashibing.ExceptionDemo.show(ExceptionDemo.java:15)
at com.mashibing.ExceptionDemo.main(ExceptionDemo.java:21)
1.8 异常的分类
异常的父类是:Throwable
,它的两个直接子类是Error和Exception
- Error:仅靠程序本身无法恢复的严重错误
- Exception:由Java应用程序抛出和处理的非严重错误
Exception又分为Checked Exception(编译时异常)和Runtime Exception(运行时异常)
- Checked异常必须捕获或者声明抛出
- 运行时异常不要求必须捕获或者声明抛出
2. 常用类
2.1 基本数据类型的包装类
包装类是将基本数据类型封装到一个类中包含属性和方法,方便对象操作。包装类位于java.lang
包中。
-
基本数据类型转换为包装类
package com.mashibing; public class IntegerDemo { public static void main(String[] args) { int a = 10; //在使用过程中,会涉及到自动装箱和自动拆箱 Integer i = new Integer(10); //手动装箱,将基本数据类型转换成包装类 Integer i2 = Integer.valueOf(a); //手动拆箱,将包装类转换成基本数据类型 Integer i3 = i.intValue(); System.out.println(a == i); } }
true
public static Integer valueOf(int i) { // -128 =< i<= 127范围内与范围外的操作是不一样的 if (i >= IntegerCache.low && i <= IntegerCache.high) //返回cache数组里面的数 return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
public class IntegerDemo { public static void main(String[] args) { //实际上调用了valueOf方法,将基本类型转换成包装类 Integer i1 = 100; Integer i2 = 100; Integer i3 = 200; Integer i4 = 200; System.out.println(i1 == i2); System.out.println(i3 == i4); } }
true false//两个新建的Integer(200)对象的地址是不一样的
但是在Double包装类中则不一样
public static Double valueOf(double d) { return new Double(d); }
public class IntegerDemo { public static void main(String[] args) { Double d1 = 1.0; Double d2 = 1.0; Double d3 = 2.0; Double d4 = 2.0; System.out.println(d1 == d2); System.out.println(d3 == d4); } }
//返回新的对象 false false
-
包装类转换成基本类型
//对象 Integer i = new Integer(10); //将包装类转换成基本类型 int i3 = i.intValue();
-
基本类型和包装类的自动转换(包装类并不是用来取代基本类型的)
Integer intObject = 5; int intValue = intObject;
2.1.1 自动拆箱和自动装箱(auto-boxing & unboxing)
-
自动装箱:基本类型自动地封装到与它相同类型的包装中
Integer i = 100;
编译器在编译时默认添加了
Integer i = Integer.ValueOf(100);
-
自动拆箱:包装对象自动转换成基本数据类型
int a = new Integer(100);
编译器在编译时为我们添加了:
int a = new Integer(100).intValue();
2.2 字符串相关类
String类位于java.lang
包中,具有丰富的方法,包括计算字符串的长度、比较字符串、连接字符串、提取字符串。
/*
* 注意:常量池在1.7后放置在了堆空间之中
* 字符串使用:
* 1.创建(两种方式)
* String str = "abc"; //地址指向“abc”的内存空间
* String str2 = new String("abc"); //地址指向String对象的内存空间
* 2.字符串的本质:
* 字符串的本质:字符串本身是字符数组(序列)
* String类使用final修饰,不可以被继承
* 使用equals方法比较的是字符数组的每一个位置的值
String是一个不可变对象,不可变指的是数组引用关系不可变
* */
package com.mashibing;
public class StringDemo {
public static void main(String[] args) {
//两种创建方式
String str = "abc";
String str2 = new String("abc");
// str2 = str2.intern();
System.out.println(str == str2); //false
System.out.println(str.equals(str2)); //true
}
}
-
比较字符串:equals
public boolean equals(Object anObject) { if (this == anObject) { return true; } //不相等的话 if (anObject instanceof String) { String aString = (String)anObject; if (coder() == aString.coder()) { return isLatin1() ? StringLatin1.equals(value, aString.value) : StringUTF16.equals(value, aString.value); } } return false; } //StringLatin1.equals public static boolean equals(byte[] value, byte[] other) { if (value.length == other.length) { for (int i = 0; i < value.length; i++) { if (value[i] != other[i]) { return false; } } return true; } return false; }
-
hashcode
public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { hash = h = isLatin1() ? StringLatin1.hashCode(value) : StringUTF16.hashCode(value); } return h; } //StringLatin1.hashCode public static int hashCode(byte[] value) { int h = 0; for (byte v : value) { //31=11111(二进制),直接进行移位运算,计算效率最高。 //&表示按位与,只有两个位同时为1,才能得到1, 0x代表16进制数,0xff表示的数二进制1111 1111 占一个字节.和其进行&操作的数,最低8位,不会发生变化. h = 31 * h + (v & 0xff); } return h; }
-
提取字符串:charAt
public char charAt(int index) { if (isLatin1()) { return StringLatin1.charAt(value, index); } else { return StringUTF16.charAt(value, index); } } //StringLatin1.charAt public static char charAt(byte[] value, int index) { if (index < 0 || index >= value.length) { throw new StringIndexOutOfBoundsException(index); } return (char)(value[index] & 0xff); }
-
拼接字符串:concat
public String concat(String str) { if (str.isEmpty()) { return this; } if (coder() == str.coder()) { byte[] val = this.value; byte[] oval = str.value; int len = val.length + oval.length; byte[] buf = Arrays.copyOf(val, len); System.arraycopy(oval, 0, buf, val.length, oval.length); return new String(buf, coder); } int len = length(); int olen = str.length(); byte[] buf = StringUTF16.newBytesFor(len + olen); getBytes(buf, 0, UTF16); str.getBytes(buf, len, UTF16); return new String(buf, UTF16); }
-
获取指定元素的下标:indexOf
public int indexOf(String str) { if (coder() == str.coder()) { return isLatin1() ? StringLatin1.indexOf(value, str.value) : StringUTF16.indexOf(value, str.value); } if (coder() == LATIN1) { // str.coder == UTF16 return -1; } return StringUTF16.indexOfLatin1(value, str.value); } //StringLatin1.indexOf public static int indexOf(byte[] value, byte[] str) { if (str.length == 0) { return 0; } if (value.length == 0) { return -1; } return indexOf(value, value.length, str, str.length, 0); }
2.2.1 常用方法总结
-
char charAt(int index)
返回字符串中第index个字符。
-
boolean equals(String other)
如果字符串与other相等,返回true
-
boolean equalsIgnoreCase(String other)
如果字符串与other相等(忽略大小写),则返回true
-
int indexOf(String str)
返回字符对应的下标值
-
int length()
返回字符串的长度。
-
String replace(char oldChar,char newChar)
返回一个新字符串,它是通过用 newChar 替换此字符串中出现的所有oldChar而生成的
-
boolean startsWith(String prefix)
如果字符串以prefix开始,则返回true
-
boolean endsWith(String prefix)
如果字符串以prefix结尾,则返回true
-
String substring(int beginIndex,int endIndex)
返回一个新字符串,该串包含从原始字符串beginIndex到串尾endIndex-1的所有字符(左闭右开)
-
String toLowerCase()
返回一个新字符串,该串将原始字符串中的所有大写字母改成小写字母
-
String toUpperCase()
返回一个新字符串,该串将原始字符串中的所有小写字母改成大写字母
-
String trim()
返回一个新字符串,该串删除了原始字符串头部和尾部的空格
-
split(),将一个字符串分割为子字符串,结果作为字符串数组返回
-
intern()
public class StringDemo { public static void main(String[] args) { //两种创建方式 String str = "abc"; String str2 = new String("abc"); str2 = str2.intern(); System.out.println(str == str2); //true } }
在没有使用intern()方法时,比较的是常量池与栈中“abc”的地址。当使用intern()方法时,如果“abc”在常量池中存放着,那么会将常量池中“abc”的地址赋予str2,因此会true。
public class StringDemo { public static void main(String[] args) { String a = "abc"; String b = "def"; String c = "abcdef"; String e = a + b; String f = (a + b).intern(); String g = "abc" + "def"; System.out.println(c == e); System.out.println(c == f); System.out.println(c == g); } }
false true true
在常量池中"abcdef"只能存在一个。
2.2.2 StringBuffer类与StringBuilder类
StringBuffer类与StringBuilder类是可变字符串。StringBuffer类是String增强版,字符串的缓冲区,是一个容器。工作中用到需要拼接字符串,一般使用StringBuffer类。
- String:不可变字符序列
- StringBuilder:可变字符序列、效率高、线程不安全
- StringBuffer:可变字符序列、效率低、线程安全(synchronized加锁)
package com.mashibing;
public class StringBufferDemo {
public static void main(String[] args) {
//创建空的StringBuffer对象
StringBuffer stringBuffer = new StringBuffer();
//追加字符串
stringBuffer.append(1).append(1.234).append("abc").append(true);
System.out.println(stringBuffer);
//charAt、indexof、length、substring、capacity、delete、insert、replace、reverse
//表示占用长度
System.out.println(stringBuffer.length());
//表示容量
System.out.println(stringBuffer.capacity());
StringBuilder StringBuilder = new StringBuilder();
StringBuilder.append("123").append(1).append(false);
System.out.println(StringBuilder);
}
}
11.234abctrue
13
16
1231false
2.3 时间处理相关类
- Calendar类
抽象类,用于设置和获取日期/时间数据的特定部分,Calendar类提供一些方法和静态字段来操作日历
- Date类
表示日期和时间,提供操作日期和时间各组成部分的方法 - DateFormat类 与SimpleDateFormat类
用于定制日期时间的格式
package com.mashibing;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println(date);
//返回当前date时间对应的毫秒值
System.out.println(date.getTime());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
//将Date类按照规范转换为字符串格式
String string = dateFormat.format(date);
System.out.println(string);
//将字符串转换成对应的日期类
Date date1 = dateFormat.parse("2010-10-10 20:20:20");
System.out.println(date1);
//格林日志的日期类,getInstance()获取的是当前系统的时间
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
//设置指定时间的日历类
calendar.setTime(date1);
System.out.println(calendar);
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));
System.out.println(calendar.get(Calendar.SECOND));
}
}
Fri Apr 08 20:48:02 GMT+08:00 2022
1649422082700
2022-04-98 20:48:02
Sun Jan 10 20:20:20 GMT+08:00 2010
java.util.GregorianCalendar[time=1649422082733,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+08:00",offset=28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=3,WEEK_OF_YEAR=15,WEEK_OF_MONTH=2,DAY_OF_MONTH=8,DAY_OF_YEAR=98,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=48,SECOND=2,MILLISECOND=733,ZONE_OFFSET=28800000,DST_OFFSET=0]
java.util.GregorianCalendar[time=1263126020000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+08:00",offset=28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=0,WEEK_OF_YEAR=3,WEEK_OF_MONTH=3,DAY_OF_MONTH=10,DAY_OF_YEAR=10,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=20,SECOND=20,MILLISECOND=0,ZONE_OFFSET=28800000,DST_OFFSET=0]
2010
0
10
20
20
20
常用的方法是:
- format:将Date类按照规范转换为字符串格式
- parse:将字符串转换成对应的日期类
2.4 Math类
Math类包含了常见的数学运算函数。
package com.mashibing;
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.abs(-1)); //绝对值
System.out.println(Math.sqrt(4)); //开方
System.out.println(Math.ceil(3.14)); //向上取整
System.out.println(Math.floor(3.14)); //向下取整
System.out.println(Math.random()); //生成[0,1)之间的随机浮点数
System.out.println(Math.round(3.145)); //四舍五入
System.out.println(Math.pow(2,3)); //幂
int b = 20 + (int)(10*Math.random()); //生成20-30之间的任意整数
System.out.println(b);
}
}
1
2.0
4.0
3.0
0.18465161177117695
3
8.0
27
2.5 枚举类
枚举指由一组固定的常量组成的类型。枚举类型:
- 只能够取特定值中的一个
- 使用enum关键字
- 所有的枚举类型隐性地继承自 java.lang.Enum(枚举实质上还是类!而每个被枚举的成员实质就是一个枚举类型的实例,他们默认都是public static final的。可以直接通过枚举类型名直接使用它们)
- 强烈建议当你需要定义一组常量时,使用枚举类型
package com.mashibing;
//枚举类
public enum EventEnum {
//以前new对象,在枚举类中已经提供对象
//枚举类也是类,包含方法
LAUNCH("launch"), PAGEVIEW("pageview"), EVENT("event");
//构造方法
EventEnum(String name) {
this.name = name;
}
private String name;
public void show() {
System.out.println(this.name);
EventEnum[] eventEnums = values();
for (int i = 0; i < eventEnums.length; i++) {
System.out.println(eventEnums[i]);
}
}
public static void main(String[] args) {
EventEnum eventEnum = EventEnum.LAUNCH;
eventEnum.show();
}
}
launch
LAUNCH
PAGEVIEW
EVENT