枚举类通过关键字enum来定义
jdk1.5版本之后出现的一个新特性
枚举类对象的属性被private final 所修饰
构造函数默认被private 修饰
枚举类对象的创建必须放在第一行进行声明
每一个对象用,隔开,最后一个用;结尾
枚举类可以实现接口,但是不能继承别的类了,因为默认继承的是java.lang.Enum
char c = ‘a’;
switch©{
case ‘a’:
break;
…
default:
break;
}
枚举类常用方法:
values(); 返回是枚举类型的数组对象
valueOf(String str);返回的是一个枚举
package com.jp01;
public class Demo {
public static void main(String[] args) {
Status newSatus = Status.NEW;
System.out.println(newSatus.getValue());
}
}
//枚举类
//描述线程整个生命周期
class Status{
//1.提供类的属性,声明为private final
private final String name;
private final Integer value;
//2.在构造函数中初始化数据
private Status(String name, Integer value){
this.name = name;
this.value = value;
}
//3.通过公共方法来获取属性值
public String getName() {
return name;
}
public Integer getValue() {
return value;
}
//新建状态
public static final Status NEW = new Status("新建状态", 0);
//运行状态 ctrl + shift + x 小写字母变大写 ctrl + shift +y 大写变小写
public static final Status RUNNABLE = new Status("运行状态", 1);
//阻塞状态
public static final Status BLOCKED = new Status("阻塞状态", 2);
//睡眠状态
public static final Status WAITING = new Status("睡眠状态", 3);
//消亡状态
public static final Status TERMINATED = new Status("消亡状态", 4);
}
package com.jp02;
public class Demo {
public static void main(String[] args) {
Status newStatus = Status.NEW;
Status[] values = newStatus.values();
for (Status s : values) {
// System.out.println(s);
}
Status blocked = newStatus.valueOf("BLOCKED");
System.out.println(blocked.getValue());
// System.out.println(newStatus.getValue());
// info in = Status.NEW;
// in.show();
}
}
interface info {
void show();
}
// 枚举类
enum Status {
NEW("新建状态", 0),
RUNNABLE("运行状态", 1),
BLOCKED("阻塞状态", 2),
WAITING("睡眠状态", 3),
TERMINATED("消亡状态", 4);
// 1.提供类的属性,声明为private final
private final String name;
private final Integer value;
// 2.在构造函数中初始化数据
private Status(String name, Integer value) {
this.name = name;
this.value = value;
}
// 3.通过公共方法来获取属性值
public String getName() {
return name;
}
public Integer getValue() {
return value;
}
}
package com.jp03;
import java.util.ArrayList;
import java.util.List;
/*
* 注解: jdk1.5版本之后出现的新特性
*
* @Deprecated 过时
* @Override 重写
* @SuppressWarnings 压缩警告
*
* 总结:相当于一种标记,可以应用到包,类,属性,方法,参数等地方
*
*/
public class Demo1 {
public static void main(String[] args) {
Person p = new Student();
p.show();
@SuppressWarnings({ "rawtypes", "unused" })
List list = new ArrayList();
}
}
@MyAnnotation(value="zs", arr="xx")
class Person{
public void teach(String english){
int i = 0;
}
public void show(){
System.out.println("人类表演...");
}
}
class Student extends Person{
@Override
public void show() {
System.out.println("学生表演...");
}
@Deprecated
public void run(){
}
public void runNew(){
}
}
package com.jp03;
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.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*
* 注解作用目标
* 类上
* 方法上
* 形参上
* 成员变量上
* 构造函数上
* 包上(需要进行特殊处理)
* 局部变量
*
* RetentionPolicy.SOURCE 只在源代码中存在,当编译完,就不存在了
* RetentionPolicy.CLASS 编译完还存在,当虚拟机加载类的时候,就不存在了
* RetentionPolicy.RUNTIME 当虚拟机加载类,运行类的时候,一直存在
*/
//元注解 为了给注解进行注解的一种注解
@Target({ElementType.TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
//注解的属性
//格式: 类型 属性值名();
//String, 基本数据类 ,Class,enum, 注解类型, 以上这些一维数据类型
/*int a();
String b();
Class c();
Myenum d();
MyAnn e();
MyAnn[] xx();*/
String value();
int age() default 18;
String[] arr();
}
enum Myenum{
A,B,C;
}
@interface MyAnn {
}