大数据必学Java基础(八十七):枚举的深入了解
【摘要】 枚举的深入了解一、引入1、数学:枚举法1<x<42<y<5求x+y=6枚举法:一枚一枚的列举出来。前提:有限,确定2、在java中,类的对象是有限个,确定的。这个类我们可以定义为枚举类举例:星期:一二三四五六日性别:男女季节:春夏秋冬3、自定义枚举类(JDK1.5之前自定义枚举类) package com.lanson.enum01;/** * @author : Lansonli * 定...
枚举的深入了解
一、引入
1、数学:枚举法
1<x<4
2<y<5
求x+y=6
枚举法:一枚一枚的列举出来。前提:有限,确定
2、在java中,类的对象是有限个,确定的。这个类我们可以定义为枚举类
举例:
星期:一二三四五六日
性别:男女
季节:春夏秋冬
3、自定义枚举类(JDK1.5之前自定义枚举类)
package com.lanson.enum01;
/**
* @author : Lansonli
* 定义枚举类:季节
*/
public class Season {
//属性:
private final String seasonName ;//季节名字
private final String seasonDesc ;//季节描述
//利用构造器对属性进行赋值操作:
//构造器私有化,外界不能调用这个构造器,只能Season内部自己调用
private Season(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//提供枚举类的有限的 确定的对象:
public static final Season SPRING = new Season("春天","春暖花开");
public static final Season SUMMER = new Season("夏天","烈日炎炎");
public static final Season AUTUMN = new Season("秋天","硕果累累");
public static final Season WINTER = new Season("冬天","冰天雪地");
//额外因素:
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//toString();
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
测试类:
public class TestSeason {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
Season summer = Season.SUMMER;
System.out.println(summer/*.toString()*/);
System.out.println(summer.getSeasonName());
}
}
二、JDK1_5之后使用enum关键字来创建枚举类
JDK1.5以后使用enum关键字创建枚举类:
变为下面的枚举类:
package com.lanson.enum02;
/**
* @author : Lansonli
* 定义枚举类:季节
*/
public enum Season {
//提供枚举类的有限的 确定的对象:--->enum枚举类要求对象(常量)必须放在最开始位置
//多个对象之间用,进行连接,最后一个对象后面用;结束
SPRING("春天","春暖花开"),
SUMMER("夏天","烈日炎炎"),
AUTUMN("秋天","硕果累累"),
WINTER("冬天","冰天雪地");
//属性:
private final String seasonName ;//季节名字
private final String seasonDesc ;//季节描述
//利用构造器对属性进行赋值操作:
//构造器私有化,外界不能调用这个构造器,只能Season内部自己调用
private Season(String seasonName, String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//额外因素:
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//toString();
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
使用枚举类:
public class TestSeason {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
Season winter = Season.WINTER;
System.out.println(winter);
//enum关键字对应的枚举类的上层父类是 :java.lang.Enum
//但是我们自定义的枚举类的上层父类:Object
System.out.println(Season.class.getSuperclass().getName());//java.lang.Enum
}
}
在源码中经常看到别人定义的枚举类形态:
public enum Season {
SPRING,
SUMMER,
AUTUMN,
WINTER;
}
为什么这么简单:因为这个枚举类底层没有属性,属性,构造器,toString,get方法都删掉不写了,然后案例来说应该
写为:SPRING() 现在连()可以省略 就变成 SPRING
看到的形态就剩:常量名(对象名)
案例:
Thread中的枚举类:State
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
三、Enum类的常用方法
package com.lanson.enum03;
/**
* @author : Lansonli
*/
public class TestSeason {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
//用enum关键字创建的Season枚举类上面的父类是:java.lang.Enum,常用方法子类Season可以直接拿过来使用:
//toString();--->获取对象的名字
Season autumn = Season.AUTUMN;
System.out.println(autumn/*.toString()*/);//AUTUMN
System.out.println("--------------------");
//values:返回枚举类对象的数组
Season[] values = Season.values();
for(Season s:values){
System.out.println(s/*.toString()*/);
}
System.out.println("--------------------");
//valueOf:通过对象名字获取这个枚举对象
//注意:对象的名字必须传正确,否则抛出异常
Season autumn1 = Season.valueOf("AUTUMN");
System.out.println(autumn1);
}
}
四、枚举类实现接口
定义一个接口:
public interface TestInterface {
void show();
}
枚举类实现接口,并且重写show方法:
public enum Season implements TestInterface {
SPRING,
SUMMER,
AUTUMN,
WINTER;
@Override
public void show() {
System.out.println("这是Season....");
}
}
测试类:
public class Test {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
autumn.show();
Season summer = Season.SUMMER;
summer.show();
}
}
上面发现所有的枚举对象,调用这个show方法的时候走的都是同一个方法,结果都一样:
但是现在我想要不同的对象 调用的show方法也不同:
package com.lanson.enum04;
import java.sql.SQLOutput;
public enum Season implements TestInterface {
SPRING{
@Override
public void show() {
System.out.println("这是春天。。。");
}
},
SUMMER{
@Override
public void show() {
System.out.println("这是夏天。。");
}
},
AUTUMN{
@Override
public void show() {
System.out.println("这是秋天");
}
},
WINTER{
@Override
public void show() {
System.out.println("这是冬天");
}
};
/*@Override
public void show() {
System.out.println("这是Season....");
}*/
}
测试类:
public class Test {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
autumn.show();
Season summer = Season.SUMMER;
summer.show();
}
}
五、实际应用
package com.lanson.enum05;
/**
* @author : Lansonli
*/
public class Person {
//属性:
private int age;
private String name;
private Gender sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getSex() {
return sex;
}
public void setSex(Gender sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public enum Gender {
男,
女;
}
public class Test {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
Person p = new Person();
p.setAge(19);
p.setName("lili");
p.setSex(Gender.男);//传入枚举类Gender的对象:-->在入口处对参数进行了限制
System.out.println(p);
}
}
还可以通过枚举结合switch处理:
public class Test02 {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
Gender sex = Gender.男;
//switch后面的()中可以传入枚举类型
//switch后面的():int,short,byte,char,String ,枚举
switch (sex){
case 女:
System.out.println("是个女孩");
break;
case 男:
System.out.println("是个男孩");
break;
}
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)