其他分享
首页 > 其他分享> > 枚举&注解

枚举&注解

作者:互联网

一、枚举&注解

如何自定义枚举类 

public class TestSeason {
        Season spring = Season.SPRING;
}

class Season {
	// 1.提供类的属性,声明为private final 
	private final String seasonName;
	private final String seasonDesc;      

	// 2.声明final的属性,在构造器中初始化
	private Season(String seasonName,String seasonDesc) {
           this.seasonName = seasonName;
           this.seasonDesc = seasonDesc;
        }      

	// 3.通过公共的方法来调用属性
	public String getSeasonName() {
            return seasonName;
        }

	public String getSeasonDesc() {
            return seasonDesc;
        }      
	// 4.创建枚举类的对象:将类的对象声明为public static final

	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", "白雪皑皑");

	@Override
    public String toString() {
           return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
    }
}

如何使用enum关键字定义枚举类

1.常用的方法


public class TestSeason {
    Season spring = Season.SPRING;
    //1.valus()方法
    Season[] seasons = Season.values();
    //2.valueOf(String name):要求传入的形参name是枚举类对象的名字
    //否则,报java.lang.IllegalArgumentException异常
    String str = "SPRING";
    Season season = Season.valueOf(str);
    Thread.State[] states = Thread.State.values();
}

enum Season {
	SPRING("spring", "春暖花开"),    
	SUMMER("summer", "夏日炎炎"),    
	AUTUMN("autumn", "秋高气爽"),    
	WINTER("winter","白雪皑皑");   
	private final String seasonName;   
	private final String seasonDesc;   

	private Season(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 "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
    }
}

 

2.如何让枚举类实现接口:可以让不同的枚举类的对象调用被重写的抽象方法,执行的效果不同。(相当于让每个对象重写抽象方法)

    interface Infomation{

    public abstract void show();

}

enum Season implements Infomation{

    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 seasonName;

    private final String seasonDesc;

   

    private Season(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 "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";

        }

}

注解Annotation

1.注解(Annotation)概述

从JDK5.0开始,Java增加了对元数据(MetaData)的支持,也就是Annotation(注解)

Annotation其实就是代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。通过使用Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。

Annotation可以像修饰符一样被使用,可用于修饰包,类,构造器,方法,成员变量,参数,局部变量的声明,这些信息被保存在Annotation的“name=value”对中

Annotation能被用来为程序元素(类,方法,成员变量等)设置元数据

2.使用Annotation时要在其前面增加@符号,并把该Annotation当成一个修饰符使用。用于修饰它支持的程序元素。三个基本的Annotation

@Override:限定重写父类方法,该注释只能用于方法

@Deprecated:用于表示某个程序元素(类,方法等)已过时

@SuppressWarnings:抑制编译器警告。

3.如何自定义一个注解

              public @interface MyAnnotation {

              String value() default "hello";

              }

4.元注解。JDK的元Annotation用于修饰其他Annotation的定义。JDK5.0提供了专门在主街上的注解类型,分别是

Retention

  

Target

Documented

Inherited

                    

标签:String,Season,枚举,Annotation,seasonName,seasonDesc,注解,public
来源: https://blog.csdn.net/weixin_44145972/article/details/88926536