7.枚举类和注解
7.1 枚举类
class Season{
private final String seasonName;
private final String seasonDesc;
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
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 + '\'' +
'}';
}
}
enum Season1{
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
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;
}
}
interface Info{
void show();
}
enum Season2 implements Info{
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
private final String seasonName;
private final String seasonDesc;
private Season2(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public void show() {
System.out.println("春夏秋冬");
}
}
enum Season3 implements Info{
SPRING("春天","春暖花开"){
@Override
public void show() {
System.out.println("春天");
}
},
SUMMER("夏天","夏日炎炎"){
@Override
public void show() {
System.out.println("夏天");
}
},
AUTUMN("秋天","秋高气爽"){
@Override
public void show() {
System.out.println("秋天");
}
},
WINTER("冬天","冰天雪地"){
@Override
public void show() {
System.out.println("冬天");
}
};
private final String seasonName;
private final String seasonDesc;
private Season3(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
public class Main {
@Test
public void test1(){
Season2.SPRING.show();
Season2.SUMMER.show();
Season2.AUTUMN.show();
Season2.WINTER.show();
Season3.SPRING.show();
Season3.SUMMER.show();
Season3.AUTUMN.show();
Season3.WINTER.show();
}
@Test
public void test2(){
Season3[] values = Season3.values();
for(int i=0;i< values.length;i++){
System.out.println(values[i]);
}
Season3 winter = Season3.valueOf("WINTER");
System.out.println(winter);
System.out.println(winter);
}
}
7.2 注解
public class Main {
@Test
public void test1(){
@SuppressWarnings("unused")
int num=10;
}
}
@Retention(RetentionPolicy.CLASS)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@interface MyAnnotation1{
String[] value();
}
@MyAnnotation1({"hello","jj"})
class Person{
}
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@interface MyAnnotation2{
String[] value() default "hello";
}
@MyAnnotation2
class Boy{
}
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@Inherited
@interface MyAnnotation3{
}
@MyAnnotation3
class Girl{
}
class GirlStudent extends Girl{
@Test
public void test() {
Class<Person> personClass = Person.class;
Annotation[] annotations1 = personClass.getAnnotations();
for(int i=0;i<annotations1.length;i++){
System.out.println(annotations1[i]);
}
System.out.println("*****");
Class<Boy> boyClass = Boy.class;
Annotation[] annotations2 = boyClass.getAnnotations();
for(int i=0;i< annotations2.length;i++){
System.out.println(annotations2[i]);
}
System.out.println("*****");
Class<Girl> girlClass = Girl.class;
Annotation[] annotations3 = girlClass.getAnnotations();
for(int i=0;i< annotations3.length;i++){
System.out.println(annotations3[i]);
}
Class<GirlStudent> girlStudentClass = GirlStudent.class;
Annotation[] annotations4 = girlStudentClass.getAnnotations();
for(int i=0;i<annotations4.length;i++){
System.out.println(annotations4[i]);
}
}
}
@Repeatable(MyAnnotation4s.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@interface MyAnnotation4{
String[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@interface MyAnnotation4s{
MyAnnotation4[] value();
}
@MyAnnotation4({"simple","wu"})
@MyAnnotation4("hello")
class Student{
}
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE,TYPE_PARAMETER,TYPE_USE})
@interface MyAnnotation5{
}
class Dog<@MyAnnotation5 T>{
public void show() throws @MyAnnotation5 RuntimeException{
ArrayList<@MyAnnotation5 String> list=new ArrayList<>();
int num=(@MyAnnotation5 int)10L;
}
}