一.枚举
1.什么是枚举类?
——使用enum关键字修饰的类叫做枚举类
2.格式
enum 枚举明 {//首行必须是枚举对象//属性//构造器//方法}
3.自定类实现枚举
- 将构造器私有化
- 只提供公共的getter方法,不提供setter方法
- 枚举对象和属性使用public + static进行修饰【类不会加载】
- 枚举也是常量,常量名需要大写
public class Test {public static void main(String[] args) {System.out.println(Season.SPRING);System.out.println(Season.SUMMER);System.out.println(Season.AUTUMN);System.out.println(Season.WINTER);}
}class Season {public final static Season SPRING = new Season("春天", "万物复苏");public final static Season SUMMER = new Season("夏天", "天气炎炎");public final static Season AUTUMN = new Season("秋天", "心中凉爽");public final static Season WINTER = new Season("冬天", "万物沉睡");//属性private String name;private String describe;//构造器私有化private Season(String name, String describe) {this.name = name;this.describe = describe;}//getter方法public String getName() {return name;}public String getDescribe() {return describe;}//toString方法public String toString() {return name + " " + describe;}
}
4.一个小case快速了解枚举【enum】类
public class Test {public static void main(String[] args) {System.out.println(Season.SPRING);System.out.println(Season.SUMMER);System.out.println(Season.AUTUMN);System.out.println(Season.WINTER);}
}enum Season {//枚举对象SPRING("春天", "万物复苏"), SUMMER("夏天", "天气炎炎"),AUTUMN("秋天", "心中凉爽"), WINTER("冬天", "万物沉睡");//属性private String name;private String describe;//构造器私有化private Season(String name, String describe) {this.name = name;this.describe = describe;}//getter方法public String getName() {return name;}public String getDescribe() {return describe;}//提供toString方法public String toString() {return name + " " + describe;}
}
5.枚举的细节讨论及实现
- 当要使用enum来开发一个枚举类时,这个类自动的继承了Enum类,且这个类是final的
- 当使用枚举对象的时候,要清楚调用的是哪个构造器【无参构造器只需要写枚举对象名】
- 当有多个枚举对象的时候,使用逗号间隔,最好需要有一个分号结尾
- 枚举对象必须放在枚举类的行首
public class Test {public static void main(String[] args) {}
}enum Season {//无参构造器只需要写对象名//枚举对象必须放在枚举类的行首SPRING("春天", "万物复苏"), SUMMER, AUTUMN;//属性private String name;private String describe;//构造器私有化private Season() {}private Season(String name, String describe) {this.name = name;this.describe = describe;}
}
6.Enum常用方法
- toString方法:子类进行重写,返回当前对象属性的信息
public class Test {public static void main(String[] args) {System.out.println(Season.SPRING);}
}enum Season {SPRING("春天", "万物复苏");//属性private String name;private String describe;//构造器private Season(String name, String describe) {this.name = name;this.describe = describe;}//重写toString方法public String toString() {return name + " " + describe;}
}
- name方法:返回当前对象名(常量名),子类不能重写
public class Test {public static void main(String[] args) {System.out.println(Season.SPRING.name());//SPRING}
}enum Season {SPRING("春天", "万物复苏");//属性private String name;private String describe;//构造器private Season(String name, String describe) {this.name = name;this.describe = describe;}
}
- ordinal:返回当前对象的位置号,默认从0开始
public class Test {public static void main(String[] args) {System.out.println(Season.SPRING.ordinal());//0System.out.println(Season.SUMMER.ordinal());//1System.out.println(Season.AUTUMN.ordinal());//2System.out.println(Season.WINTER.ordinal());//3}
}enum Season {SPRING("春天", "万物复苏"), SUMMER("夏天", "天气炎炎"),AUTUMN("秋天", "心中凉爽"), WINTER("冬天", "万物沉睡");//属性private String name;private String describe;//构造器private Season(String name, String describe) {this.name = name;this.describe = describe;}
}
- values:返回当前枚举类中所有的常量
public class Test {public static void main(String[] args) {Season[] seasons = Season.values();for (int i = 0; i < seasons.length; i++) {System.out.println(seasons[i]);}}
}enum Season {SPRING("春天", "万物复苏"), SUMMER("夏天", "天气炎炎"),AUTUMN("秋天", "心中凉爽"), WINTER("冬天", "万物沉睡");//属性private String name;private String describe;//构造器private Season(String name, String describe) {this.name = name;this.describe = describe;}
}
- valueOf:将字符串转换成枚举对象,要求字符串必须为已有的常量名,否则报错
public class Test {public static void main(String[] args) {System.out.println(Season.OTHER);}
}enum Season {SPRING("春天", "万物复苏"), SUMMER("夏天", "天气炎炎"),AUTUMN("秋天", "心中凉爽"), WINTER("冬天", "万物沉睡");//属性private String name;private String describe;//将字符串转换成枚举对象public final static Season OTHER = Season.valueOf("SPRING");//构造器private Season(String name, String describe) {this.name = name;this.describe = describe;}
}
- compareTo:比较两个枚举常量,比较的是编号【返回的是两者之差】
public class Test {public static void main(String[] args) {System.out.println(Season.SPRING.compareTo(Season.SUMMER));//-1System.out.println(Season.SPRING.compareTo(Season.AUTUMN));//-2System.out.println(Season.SPRING.compareTo(Season.WINTER));//-3System.out.println(Season.WINTER.compareTo(Season.SPRING));//3}
}enum Season {SPRING("春天", "万物复苏"), SUMMER("夏天", "天气炎炎"),AUTUMN("秋天", "心中凉爽"), WINTER("冬天", "万物沉睡");//属性private String name;private String describe;//构造器private Season(String name, String describe) {this.name = name;this.describe = describe;}
}
二.注解
1.注解的理解
- 注解(元数据),用于修饰解释包、类、构造器、方法、属性、局部变量等数据信息
- 注释不能被编译或运行,注解可以被编译或运行,相当于嵌入在到吗中的补充信息
2.基本的注解介绍
使用注解的时候需要在其前面加@符号,并把对应的注解当做一个修饰符使用【即修饰支持注解的程序元素】
- @Override:该注解只能重写父类的方法【限定方法】
- @Deprecated:表示某个程序元素(类/方法)已经过时
- @SuppressWarnings:抑制编译器的警告
3.一个小case快速了解注解
public class Test {public static void main(String[] args) {Kid kid = new Kid();kid.say();System.out.println(kid.name);}
}@SuppressWarnings({"all"})//把所有的警告镇压【该类不会报警告】
class Parent {@Deprecated//表示这个name已经过时了public String name = "jack";public void say() {System.out.println("父亲说:我为我的儿子感到骄傲~");}
}@SuppressWarnings({"all"})
class Kid extends Parent {private int age = 10;@Override//该注解只能用于重写父类方法public void say() {System.out.println("孩子说:我要努力学习~");}
}
4.注解的细节讨论及实现
- @Override的细节
1> @Override表示重写父类的方法【编译方面验证】,如果使用@Override后但是没有重写父类方法,则编译器报错
2> 如果不写@Override,但是子类中还是有重写的方法,也构成重写
3> @Override只能修饰方法,不能修饰其他的
public class Test {public static void main(String[] args) {Kid kid = new Kid();kid.say();kid.hi();}
}class Parent {public String name = "jack";public void say() {System.out.println("父亲说:我为我的儿子感到骄傲~");}public void hi() {System.out.println("hi~");}
}class Kid extends Parent {@Override//该注解只能用于重写父类方法public void say() {System.out.println("孩子说:我要努力学习~");}//没有使用@Override也构成了重写//@Override只能修饰方法public void hi() {System.out.println("孩子说:hi");}//如果使用了@Override但没有重写,则保错//@Override //使用则报错public void hello() {}
}
- @Deprecated的细节
1> 某个类、方法、属性、构造器被@Deprecated修饰后,表示已过时
public class Test {public static void main(String[] args) {//使用过时的值时中间会加中划线Kid kid = new Kid();}
}class Parent {@Deprecated //修饰属性,属性已过时public String name = "jack";public void say() {System.out.println("父亲说:我为我的儿子感到骄傲~");}@Deprecated //修饰方法,方法已过时public void hi() {System.out.println("hi~");}
}@Deprecated//修饰类,即类已过时
class Kid {
}
- @SuppressWarnings的细节
1> unchecked:忽略没有检查的值
2> rawtypes:忽略没有指定泛型的警告【传参时没有指定泛型的警告错误】
3> unused:忽略没有使用某个变量的警告
import java.util.ArrayList;
import java.util.List;public class Test {@SuppressWarnings({"unused", "rawtypes"})public static void main(String[] args) {List lists = new ArrayList();}
}//@SuppressWarnings("all"): 表示把该类所有的警告全部屏蔽
@SuppressWarnings("unused")
class Parent {public String name = "jack";public void say() {System.out.println("父亲说:我为我的儿子感到骄傲~");}public void hi() {System.out.println("hi~");}
}
6.元注解
- 什么是元注解?
元注解是用于修饰其他注解的
- 元注解的类型
1> Retention:指定注解的作用范围【SOURCE,CLASS,RUNTIME】
2> Target:指定注解可以在那些地方使用
3> Documented:指定该注解是否会在javadoc中体现
4> Inherited:子类会继承父类的注解