// 这里是final修饰的,不允许子类重写
public final boolean equals(Object other) {
return this==other;
}
// Enum 中的源码
public final int compareTo(E o) {
Enum other = (Enum)o;
Enum self = this;
if (self.getClass() != other.getClass() && // optimization
self.getDeclaringClass() != other.getDeclaringClass())
throw new ClassCastException();
return self.ordinal - other.ordinal;
}
// Enum 中 name() 和 toString()
public String toString() {
return name;
}
public final String name() {
return name;
}
public enum Color {
RED, GREEN, BLANK, YELLOW
}
// JDK1.6 中switch加入了对枚举的支持
enum Signal {
GREEN, YELLOW, RED
}
...
switch (color) {
case RED:
color = Signal.GREEN;
break;
}
...
public enum Color {
RED("红色"), GREEN("绿色"), BLANK("白色"), YELLO("黄色");
// 成员变量
private String name;
// 构造方法
private Color(String name) {
this.name = name;
}
// get set 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public interface Behaviour {
void print();
}
public enum Color implements Behaviour{
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
//接口方法
@Override
public void print() {
System.out.println(this.index+":"+this.name);
}
}
public enum Operation {
// 用于执行加法运算
PLUS { // 花括号部分其实是一个匿名内部子类
@Override
public double calculate(double x, double y) {
return x + y;
}
},
// 用于执行减法运算
MINUS { // 花括号部分其实是一个匿名内部子类
@Override
public double calculate(double x, double y) {
// TODO Auto-generated method stub
return x - y;
}
},
// 用于执行乘法运算
TIMES { // 花括号部分其实是一个匿名内部子类
@Override
public double calculate(double x, double y) {
return x * y;
}
},
// 用于执行除法运算
DIVIDE { // 花括号部分其实是一个匿名内部子类
@Override
public double calculate(double x, double y) {
return x / y;
}
};
//为该枚举类定义一个抽象方法,枚举类中所有的枚举值都必须实现这个方法
public abstract double calculate(double x, double y);
}
public enum Singleton {
INSTANCE;
public Singleton getInstance(){
// 增加这个方法是让别人明白怎么使用,因为这种实现方式还比较少见。
return INSTANCE;
}
}
for(WeekDayEnum day : EnumSet.range(WeekDayEnum.Mon, WeekDayEnum.Fri)) {
System.out.println(day);
}
Map<Weather, String> enumMap = new EnumMap<Weather, String>(Weather.class); enumMap.put(Weather.Sunny, "晴天"); enumMap.put(Weather.Rainy, "雨天");
public interface QRCodeType {
int WECHAT = 0;
int ALIPAY = 1;
@IntDef({WECHAT , ALIPAY })
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@interface Checker {
}
}
public class QRCode {
@QRCodeType.Checker // 定义在属性
private int type;
public void setType(@QRCodeType.Checker int type) { // 定义在参数
this.type= type;
}
@QRCodeType.Checker // 定义在方法(也就是检查返回值的类型)
public int getType() {
return type;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有