文章目录
一、枚举介绍 二、自定义类实现枚举 三、enum关键字实现枚举 3.1 enum案例 3.2 enum关键字实现枚举注意事项 3.3 enum常用方法说明 3.4 enum实现接口
四、JDK内置的基本注解类型 4.1 注解的理解 4.2 基本的Annotation介绍 4.3 @Override 4.4 @Deprecated 4.5 @SuppressWarnings
五、元注解:对注解进行注解 5.1 元注解的基本介绍 5.2 @Retention 5.3 @Target 5.4 @Documented 5.5 Inherited注解
细节
一、枚举介绍
二、自定义类实现枚举
package com. enum_ ;
public class DefineByUser {
public static void main ( String [ ] args) {
System . out. println ( Season . AUTUMN ) ;
}
}
class Season {
public static final Season SPRING = new Season ( "Spring" , "温暖" ) ;
public static final Season SUMMER = new Season ( "Summer" , "炎热" ) ;
public static final Season AUTUMN = new Season ( "Autumn" , "凉爽" ) ;
public static final Season WINTER = new Season ( "Winter" , "寒冷" ) ;
private String name;
private String desc;
private Season ( String name, String desc) {
this . name = name;
this . desc = desc;
}
public String getName ( ) {
return name;
}
public String getDesc ( ) {
return desc;
}
@Override
public String toString ( ) {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}' ;
}
}
三、enum关键字实现枚举
3.1 enum案例
package com. enum_ ;
public class DefineByEnum {
public static void main ( String [ ] args) {
System . out. println ( Season2 . SPRING ) ;
}
}
enum Season2 {
SPRING ( "Spring" , "温暖" ) , SUMMER ( "Summer" , "炎热" ) , AUTUMN ( "Autumn" , "凉爽" ) ,
WINTER ( "冬天" , "寒冷" ) ;
private String name;
private String desc;
Season2 ( String name, String desc) {
this . name = name;
this . desc = desc;
}
public String getName ( ) {
return name;
}
public String getDesc ( ) {
return desc;
}
}
3.2 enum关键字实现枚举注意事项
要求所有的构造器均为 private
修饰(默认的无参构造器也为 private
修饰) enum反编译的信息如下
3.3 enum常用方法说明
3.4 enum实现接口
四、JDK内置的基本注解类型
4.1 注解的理解
4.2 基本的Annotation介绍
4.3 @Override
4.4 @Deprecated
4.5 @SuppressWarnings
五、元注解:对注解进行注解
5.1 元注解的基本介绍
5.2 @Retention
5.3 @Target
5.4 @Documented
5.5 Inherited注解
细节
增强for循环
public class Test {
public static void main ( String [ ] args) {
Teacher [ ] teas = { new Teacher ( "aa" , "32" ) , new Teacher ( "bb" , "32" ) , new Teacher ( "bb" , "32" ) } ;
for ( Teacher t: teas) {
System . out. println ( "name:" + t. name + " age:" + t. age) ;
}
}
}
class Teacher {
public String name;
public int age;
public Teacher ( String name, int age) {
this . name = name;
this . age = age;
}
}