枚举 enum
确定一种事物的所有可能|实例
枚举是一种特殊的类型,定义特殊,特点特殊,使用特殊
使用enum定义枚举类型
枚举类型的实例是由枚举类内部提供,不允许外部创建,只能获取使用
枚举类中的字段为当前枚举类型的实例,相当于省略了public static final
枚举类中的构造器默认修饰符为private
枚举类中可以定义字段,属性,构造器,方法....
枚举类型的使用,通过枚举类名.实例名获取对象,通过对象调用成员
枚举类型默认隐式的继承自 java.lang.Enum 这是所有Java语言枚举类型的公共基类。
定义一个员工类,
1)员工姓名,年龄,工号,入职时间,薪资,员工工作状态(忙碌,空闲,正常)
2)找到找到2021年入职的员工,打印
3)找到工作状态忙碌的员工,薪资涨%6
package com.yjxxt.method010;
/*
定义一个员工类,
1)员工姓名,年龄,工号,入职时间,薪资,员工工作状态(忙碌,空闲,正常)
2)找到找到2021年入职的员工,打印
3)找到工作状态忙碌的员工,薪资涨%6
*/
public class Class07_Enum {
public static void main(String[] args) {
Employee02[] emp = {
new Employee02("宋佳乐",18,2019110830,2021,28000,State01.s1),
new Employee02("甘峰云",18,2019110831,2021,28000,State01.s2),
new Employee02("侯昕赟",18,2019110832,2022,28000,State01.s3),
new Employee02("康洪亮",18,2019110833,2023,28000,State01.s1)
};
//遍历
for(Employee02 i:emp){
if(i.getEnterYear() == 2021){
System.out.println(i);
}
}
for (Employee02 a:emp){
if(a.getStare() == State01.s1){
a.setSalary(a.getSalary()*1.06);
System.out.println(a);
}
}
}
}
class Employee02{
//属性私有
private String name;
private int age;
private int ID;
private int enterYear;
private double salary;
private State01 stare;
//构造器
public Employee02() {
}
//带参构造
public Employee02(String name, int age, int ID, int enterYear, double salary, State01 stare) {
this.name = name;
this.age = age;
this.ID = ID;
this.enterYear = enterYear;
this.salary = salary;
this.stare = stare;
}
//设置器 访问器
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getEnterYear() {
return enterYear;
}
public void setEnterYear(int enterYear) {
this.enterYear = enterYear;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public State01 getStare() {
return stare;
}
public void setStare(State01 stare) {
this.stare = stare;
}
//重写toString
@Override
public String toString(){
return "Employee02{ "+'\n'+"姓名 :"+name+" 年龄 "+age+" ID :"+ID
+" 入职时间 "+enterYear+" 工资 "+salary+" 状态 "+stare;
}
}
enum State01{
//实例
s1("忙碌"),s2("空闲"),s3("正常");
//属性
public String state;
//构造器
State01() {
}
State01(String state) {
this.state = state;
}
//设置器 访问器
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}