Java-枚举和注解
枚举
自定义枚举类
public class Enumeration01 {
public static void main(String[] args) {
// 1. 枚举对应英文enumeration,简写enum
// 2. 枚举是一组常量的集合
// 3. 枚举属于一种特殊的类,里面只包换一组有限的特定的对象
System.out.println(Season.SPRING);
System.out.println(Season.SUMMER);
System.out.println(Season.AUTUMN);
System.out.println(Season.WINNER);
}
}
class Season {
private String name;
private String desc;
public final static Season SPRING = new Season("spring", "worm");
public final static Season SUMMER = new Season("summer", "hot");
public final static Season AUTUMN = new Season("autumn", "nice");
public final static Season WINNER = new Season("winner", "cold");
// 私有化构造器,防止直接new
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
private Season() {
}
}
enum关键字
public class Enumeration02 {
public static void main(String[] args) {
System.out.println(Season2.SPRING);
System.out.println(Season2.SUMMER);
System.out.println(Season2.AUTUMN);
System.out.println(Season2.WINNER);
}
}
enum Season2 {
// 使用enum 实现枚举类
// 1. 使用关键字enum 代替 class
// 2. 使用SPRING("spring", "warm"); 替代 public final static Season SPRING = new Season("spring", "worm");
// 3. 如果有多个,用 , 逗号间隔
// 4. 如果使用枚举enum, 常量放在最前面
// 5. 底层继承 于 Enumeration类
// 6. 采用无参构造器时,可以省略()
// 7. 不可再继承于其他类
// 8. 但可以实现其他接口
SPRING("spring", "warm"),
SUMMER("summer","hot"),
AUTUMN("autumn" , "nice"),
WINNER("winner","cold");
private String name;
private String desc;
// 私有化构造器,防止直接new
private Season2(String name, String desc) {
this.name = name;
this.desc = desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
private Season2() {
}
}
常用方法
public class EnumerationFunction {
public static void main(String[] args) {
Season3 spring = Season3.SPRING;
// 1. 输出枚举对象的名称
System.out.println(spring.name());
// 2. 输出该枚举对象列表的下标 从0开始编号
System.out.println(spring.ordinal()); // 0
// 3. values方法 返回一个对象数组 Season3[]
for (Season3 s: Season3.values()) {
System.out.println(s);
}
// 4. valueOf() 根据输入的"SUMMER"到Season3的枚举对象中查找
// 如果查找成功了,就返回,如果没找到 就报错
System.out.println(Season3.valueOf("SPRING"));
System.out.println(Season3.valueOf("SPRING") == spring); // true
// 5. compareto 比较枚举对象的下标, 相同返 前者下标-后者下标
System.out.println(Season3.SPRING.compareTo(Season3.WINNER));
}
}
enum Season3 {
SPRING("spring", "warm"),
SUMMER("summer","hot"),
AUTUMN("autumn" , "nice"),
WINNER("winner","cold");
private String name;
private String desc;
private Season3(String name, String desc) {
this.name = name;
this.desc = desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
private Season3() {
}
}
测试
public class Enumeration03 {
public static void main(String[] args) {
People p1 = People.BOY;
People p2 = People.BOY;
System.out.println(p1); // 调用父类Enum的toString方法 BOY
System.out.println(p1 == p2); // 地址相同 true
}
}
enum People{
BOY, GIRL;
}
注解
基本注解
/*
使用注解时要在其签名加上@符号,并把Annotation当成一个修饰符使用
1. @Override 限定某个方法,是重新父类方法,该注解只能用于方法
在正常使用可能不用写,但是如果写了,编译器会自动检查是否实现了重写,如果没有重写就会提醒报错
@interface--注解 不是接口
@Target(ElementType.METHOD) 只能放在方法上,修饰注解的注解
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
2. @Deprecated 用于表示某个程序元素(类,方法 等)已过时
修饰某个元素 表示某个元素已过时
不再推荐使用,但仍使用
可修饰 类 字段 方法 包 等
可用于版本升级 兼容过渡 使用
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
3. @SuppressWarnings 抑制编译器警告
当不希望看到警告时,可以使用这个注解来抑制警告信息 @SuppressWarnings
@SuppressWarnings({""}) 引号中可写入不希望看到的信息
可根据 警告 信息 写入参数, 可以填入多个,list列表
可以作用于 类 方法 语句
all -所有警告
deprecation 使用了不赞成使用的类或方法时的警告
unchecked 执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型。
fallthrough 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告。
path 在类路径、源文件路径等中有不存在的路径时的警告。
serial 当在可序列化的类上缺少 serialVersionUID 定义时的警告。
finally 任何 finally 子句不能正常完成时的警告。
all to suppress all warnings (抑制所有警告)
boxing (抑制装箱、拆箱操作时候的警告)
cast (抑制映射相关的警告)
dep-ann (抑制启用注释的警告)
deprecation (抑制过期方法警告)
fallthrough (抑制确在switch中缺失breaks的警告)
finally (抑制finally模块没有返回的警告)
hiding (抑制相对于隐藏变量的局部变量的警告)
incomplete-switch (忽略没有完整的switch语句)
nls ( 忽略非nls格式的字符)
null( 忽略对null的操作)
rawtypes( 使用generics时忽略没有指定相应的类型)
restriction ( 抑制禁止使用劝阻或禁止引用的警告)
serial ( 忽略在serializable类中没有声明serialVersionUID变量)
static-access 抑制不正确的静态访问方式警告)
synthetic-access ( 抑制子类没有按最优方法访问内部类的警告)
unchecked ( 抑制没有进行类型检查操作的警告)
unqualified-field-access ( 抑制没有权限访问的域的警告)
unused ( 抑制没被使用过的代码的警告)
*/