第8章 枚举类&注解

8、枚举及注解
8.1 如何自定义枚举类 1课时
8.2 如何使用关键字enum定义枚举类 1课时
8.3 枚举类的主要方法 1课时
8.4 实现接口的枚举类 1课时
##8-1 枚举类 #####枚举类入门 ![](https://i.imgur.com/WvWuGkq.png) #####枚举类的属性 + 枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰 + 枚举类的使用 private final 修饰的属性应该在构造器中为其赋值 + 若枚举类显式的定义了带参数的构造器, 则在列出枚举值时也必须对应的传入参数 #####enum枚举类 + 必须在枚举类的第一行声明枚举类对象。 + 枚举类和普通类的区别: + 使用 enum 定义的枚举类默认继承了java.lang.Enum 类 + 枚举类的构造器只能使用 private 访问控制符 + 枚举类的所有实例必须在枚举类中显式列出(, 分隔 ; 结尾 ). 列出的实例系统会自动添加 public static final 修饰 + JDK 1.5 中可以在 switch 表达式中使用Enum定义的枚举类的对象作为表达式, case 子句可以直接使用枚举值的名字, 无需添加枚举类作为限定 #####使用 Enum 定义的 Season ![](https://i.imgur.com/FQVxuDD.png)

jdk5.0之前案例

/**
 * 1.枚举类:一个类中的对象是有限的、可数多个的。
 *   
 *   >如果枚举类的对象只有一个,可以看做是单例模式的实现方式。
 *   
 * 2.如何自定义枚举类 ----jdk5.0之前
 * 
 * 3.如何使用enum定义枚举类 ---jdk5.0
 *    >常用的方法:values() / valueOf(String s)
 *    >如何实现接口,使得每一个枚举类的对象分别重写接口中的抽象方法
 * 
 *
 */
public class SeasonTest1 {
	public static void main(String[] args) {
		Season autumn = Season.AUTUMN;
		System.out.println(autumn);
		
		Season spring = Season.SPRING;
		System.out.println(spring.getSEASONNAME());
		System.out.println(spring.getSEASONDESC());
	}
}

//枚举类
class Season{
	//1.给类的对象声明声明,要求属性声明为:private final
	private final String SEASONNAME;//季节的名称
	private final String SEASONDESC;//季节的描述
	
	//2.私有化构造器,并给常量赋值
	private Season(String seasonName,String seasonDesc){
		this.SEASONNAME = seasonName;
		this.SEASONDESC = seasonDesc;
	}
	
	//3.提供当前枚举类的对象。声明为:public static final
	public static final Season SPRING = new Season("春天", "春暖花开");
	public static final Season SUMMER = new Season("夏天", "夏日炎炎");
	public static final Season AUTUMN = new Season("秋天", "秋高气爽");
	public static final Season WINTER = new Season("冬天", "白雪皑皑");

	public String getSEASONNAME() {
		return SEASONNAME;
	}
	public String getSEASONDESC() {
		return SEASONDESC;
	}
	@Override
	public String toString() {
		return "Season [SEASONNAME=" + SEASONNAME + ", SEASONDESC=" + SEASONDESC + "]";
	}
	
	
}

jdk5.0之后案例
//如何使用enum关键字定义枚举类
public class SeasonTest2 {
	public static void main(String[] args) {
		//1.values():返回所有枚举类对象构成的数组
		Season1[] seasons = Season1.values();
		for(int i = 0;i < seasons.length;i++){
			System.out.println(seasons[i]);
			seasons[i].show();
			
		}
		System.out.println();
		//2.valueOf(String name):根据给定的枚举类对象名获取对应的枚举类的对象
		//如果传入的枚举类对象名不准确,报java.lang.IllegalArgumentException异常。
		String name = "AUTUMN";//String name = "AUTUMN1";
		Season1 season = Season1.valueOf(name);
		System.out.println(season);
	}
}


interface Info{
	
	void show();
}

//使用enum定义枚举类
enum Season1 implements Info{
	SPRING("春天", "春暖花开"){
		public void show(){
			System.out.println("春天在哪里?");
		}
	},
	SUMMER("夏天", "夏日炎炎"){
		public void show(){
			System.out.println("宁静的夏天");
		}
	},
	AUTUMN("秋天", "秋高气爽"){
		public void show(){
			System.out.println("秋天是用来分手的季节");
		}
	},
	WINTER("冬天", "白雪皑皑"){
		public void show(){
			System.out.println("2002年的第一场雪");
		}
	};
	
	
	private final String SEASONNAME;//季节的名称
	private final String SEASONDESC;//季节的描述
	
	private Season1(String seasonName,String seasonDesc){
		this.SEASONNAME = seasonName;
		this.SEASONDESC = seasonDesc;
	}
	

	public String getSEASONNAME() {
		return SEASONNAME;
	}
	public String getSEASONDESC() {
		return SEASONDESC;
	}
	@Override
	public String toString() {
		return "Season1 [SEASONNAME=" + SEASONNAME + ", SEASONDESC=" + SEASONDESC + "]";
	}


//	@Override
//	public void show() {
//		System.out.println("这是一个季节!");
//	}
	
	
}
Enum枚举类


实现接口的枚举类
  • 和普通 Java 类一样,枚举类可以实现一个或多个接口
  • 若需要每个枚举值在调用实现的接口方法呈现出不同的行为方式, 则可以让每个枚举值分别来实现该方法

8-2 注解(Annotation)

二、注解Annotation

主要内容

  • JDK内置的基本注解类型(3个)
  • 自定义注解类型
  • 元注解:对注解进行注解(4个)
  • 利用反射获取注解信息(在反射部分涉及)
  • Java 8 中关于注解的修改
注解 (Annotation) 概述

注解(Annotation)的使用案例

/**
 * 注解(Annotation)的使用
 * 1.注解的理解:Annotation 其实就是代码里的特殊标记, 这些标记可以在编译, 类加载, 运行时被读取, 
 * 并执行相应的处理。通过使用 Annotation, 程序员可以在不改变原有逻辑的情况下, 在源文件中嵌入一些补充信息。
 * 
 * 2.JDK 5.0 开始,提供了对元数据(Annotation)的支持
 * 
 * 3.java中常见的三个注解:
 * 
 * @Override: 限定重写父类方法, 该注解只能用于方法
 * @Deprecated: 用于表示某个程序元素(类, 方法等)已过时
 * @SuppressWarnings: 抑制编译器警告

 * 
 * 4.如何自定义注解:
 *  参照@SuppressWarnings定义即可
 * 
 * 5.元注解:JDK 的元 Annotation 用于修饰其他 Annotation 定义
 * @Retention:指明所修饰的注解的生命周期。 SOURCE,CLASS,RUNTIME
 * @Target:指明所修饰的注解可以修饰的结构。
 * 
 * @Documented:指明所修饰的注解是否可以被javadoc所解析。
 * @Inherited:指明所修饰的注解是否具有继承性
 * 
 * 6.如何在程序中获取注解信息:放在第15章的反射机制时讲。
 *    前提:要求注解的生命周期必须声明为:RUNTIME.
 * 
 * 7.java 8 中对注解的新规范:重复注解;类型注解
 *  7.1重复注解@MyAnnotation:① @MyAnnotion使用元注解:@Repeatable
 *                         ② @Repeatable的值为:另一个注解:@MyAnnotions
 *                         ③ @MyAnnotions 定义时,内部的元素为:MyAnnotation[]
 *
 */
public class AnnotationTest {
	@SuppressWarnings({ "unused", "deprecation" })
	public static void main(String[] args) {
		Person p = new Student();
		p.walk();
		
		
		Date date = new Date(2007, 3, 23);
		System.out.println(date);
		
		String s =  new String("abc");
//		System.out.println(s);
	}
}

//@MyAnnotations({@MyAnnotation("initial"),@MyAnnotation("initial")})
@MyAnnotation("initial")
@MyAnnotation("initial1")
class Person{
	
	String name;
	int age;
	
	public Person(){}
	
	public Person(String name){
		this.name = name;
	}
	
	public void eat(){
		System.out.println("吃饭");
	}
	public void walk(){
		System.out.println("走路");
	}
	
	
}

class Student extends @MyAnnotation Person{
	
	@Deprecated
	public Student(){
		
	}
	
	public Student(@MyAnnotation String name){ //Checker第三方的.jar包。 @NonNull
		super(name);
	}
	
	@Override
	public void eat() {
		super.eat();
	}
	
	@Override
	public void walk(){
		System.out.println("学生走路");
	}
	
}

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.TYPE_USE;

import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_USE})
@Retention(RetentionPolicy.CLASS)
@Repeatable(MyAnnotations.class)
public @interface MyAnnotation {
	
	String value() default "hello";
}

	import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotations {
	
	MyAnnotation[] value();
	
}
基本的 Annotation

自定义 Annotation

JDK 的元 Annotation



练 习

1.编写一个Person类,使用Override注解它的toString方法

2.自定义一个名为“MyTiger”的注解类型,它只可以使用在方法上,带一个String类型的value属性,然后在第1题中的Person类上正确使用。

提取 Annotation 信息

Java 8 中关于注解的修改

Java 8对注解处理提供了两点改进:可重复的注解可用于类型的注解。此外,反射也得到了加强,在Java8中能够得到方法参数的名称。这会简化标注在方法参数上的注解。

@Target({TYPE, FIELD, METHOD, PARAMETER,PACKAGE, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyAnnotations.class)
public @interface MyAnnotation {
	String[] value();
}

应用场景:

public Person(@MyAnnotation(value="notnull")String name){
	this.name = name;
}
原文地址:https://www.cnblogs.com/ttzzyy/p/9714380.html