java 枚举、注解
1.java枚举
1.1 jdk1.5 之前没有枚举类型,类的对象有限个,是单例模式的特殊;
自定义枚举类:
public class TestSeason {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring.toString());
}
}
/**
* 枚举类
*
*/
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;
}
// 4.创建枚举的对象,因为类的对象的个数是可以确定的,所以直接在里面创建对象就可以了
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", "白雪皑皑");
// 3.通过公关的方法进行属性调用
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public String toString() {
return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
}
}
1.2 使用enum关键字自定义枚举类
public class TestSeason1 {
public static void main(String[] args) {
Season1 spring = Season1.SPRING;
System.out.println(spring.toString());
// 1.获取枚举的所有的对象 values();
Season1[] values = Season1.values();
for (Season1 season1 : values) {
System.out.println(season1.toString());
}
// 2.valueOf(String name),要求传入的形参是枚举类对象的名字,否则报异常
String str = "SPRING";
Season1 sea = Season1.valueOf(str);
System.out.println(sea);
// 线程的内部类
State[] values2 = Thread.State.values();
for (State state : values2) {
System.out.println(state);
}
}
}
/**
* 枚举类
*
*/
enum Season1 {
SPRING("spring", "春暖花开"), SUMMER("summer", "夏日炎炎"), AUTUMN("autumn", "秋高气爽"), WINTER("winter", "白雪皑皑");
private final String Season1Name;
private final String Season1Desc;
private Season1(String Season1Name, String Season1Desc) {
this.Season1Name = Season1Name;
this.Season1Desc = Season1Desc;
}
// 3.通过公关的方法进行属性调用
public String getSeason1Name() {
return Season1Name;
}
public String getSeason1Desc() {
return Season1Desc;
}
@Override
public String toString() {
return "Season1 [Season1Name=" + Season1Name + ", Season1Desc=" + Season1Desc + "]";
}
}
1.3 如何让枚举类实现接口
public class TestSeason1 {
public static void main(String[] args) {
Season1 spring = Season1.SPRING;
spring.show();
System.out.println(spring.toString());
// 1.获取枚举的所有的对象 values();
Season1[] values = Season1.values();
for (Season1 season1 : values) {
System.out.println(season1.toString());
}
// 2.valueOf(String name),要求传入的形参是枚举类对象的名字,否则报异常
String str = "WINTER";
Season1 sea = Season1.valueOf(str);
sea.show();
System.out.println(sea);
// 线程的内部类
State[] values2 = Thread.State.values();
for (State state : values2) {
System.out.println(state);
}
}
}
interface nfo {
void show();
}
/**
* 枚举类 如何让枚举实现接口:可以让不同的枚举对象调用被重写的抽象方法,执行的效果不同(相当于让每一个对象重写抽象方法)
*/
enum Season1 implements nfo {
SPRING("spring", "春暖花开") {
public void show() {
System.out.println("春天在哪里");
}
},
SUMMER("summer", "夏日炎炎") {
public void show() {
System.out.println("生如夏花");
}
},
AUTUMN("autumn", "秋高气爽") {
public void show() {
System.out.println("秋高气爽");
}
},
WINTER("winter", "白雪皑皑") {
public void show() {
System.out.println("冬天里的一把火");
}
};
private final String Season1Name;
private final String Season1Desc;
private Season1(String Season1Name, String Season1Desc) {
this.Season1Name = Season1Name;
this.Season1Desc = Season1Desc;
}
// 3.通过公关的方法进行属性调用
public String getSeason1Name() {
return Season1Name;
}
public String getSeason1Desc() {
return Season1Desc;
}
@Override
public String toString() {
return "Season1 [Season1Name=" + Season1Name + ", Season1Desc=" + Season1Desc + "]";
}
}
2.注解
- 从jdk5.0开始,java增加对了元数据(MetaDate)的支持,也就是注解(Annotation);
- Annotation 其实就是代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理,通过Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息;
- Annotation可以像修饰符一样被使用,可用于修饰包、类、构造器、方法、成员变量、参数、局部变量的声明,这些信息被保存在Annotation的“name=value”对中。
- Annotation能被用来为程序元素(类、方法、成员变量等)设置元数据
1.jdk提供的常用的注解@suppressWarnings@Override@Deprecated
public class TestAnnotation {
public static void main(String[] args) {
Person p = new Student();
p.walk();
}
}
class Student extends Person {
@Override
public void walk() {
System.out.println("学生跑步");
}
@Override
public void eat() {
System.out.println("学生吃饭");
}
}
class Person {
String name;
int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void walk() {
System.out.println("走路");
}
public void eat() {
System.out.println("吃饭");
}
}