1.什么是枚举类?
——使用enum关键字修饰的类叫做枚举类
2.格式
enum 枚举明 {
//首行必须是枚举对象
//属性
//构造器
//方法
}
3.自定类实现枚举
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.枚举的细节讨论及实现
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常用方法
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;
}
}
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;
}
}
public class Test {
public static void main(String[] args) {
System.out.println(Season.SPRING.ordinal());//0
System.out.println(Season.SUMMER.ordinal());//1
System.out.println(Season.AUTUMN.ordinal());//2
System.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;
}
}
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;
}
}
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;
}
}
public class Test {
public static void main(String[] args) {
System.out.println(Season.SPRING.compareTo(Season.SUMMER));//-1
System.out.println(Season.SPRING.compareTo(Season.AUTUMN));//-2
System.out.println(Season.SPRING.compareTo(Season.WINTER));//-3
System.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.基本的注解介绍
使用注解的时候需要在其前面加@符号,并把对应的注解当做一个修饰符使用【即修饰支持注解的程序元素】
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.注解的细节讨论及实现
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() {
}
}
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 {
}
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:子类会继承父类的注解