Java必知基础(六)

    xiaoxiao2022-07-13  193

    枚举

    JDK1.5之前需要自定义枚举类,之后新增了enum关键字用于定义枚举类,若枚举类只有一个成员则可以作为一种单例模式的实现方式。 自定义枚举类:

    public class Custom { // 类的属性 final private final String name; private final int age; //构造器私有化 private Custom(String name, int age) { this.name = name; this.age = age; } //提供公共方法获取属性 public String getName() { return name; } public int getAge() { return age; } //实例化 将类的对象声明为 public static final public static final Custom XIAOMING = new Custom("小明",18); public static final Custom XIAOWANG = new Custom("小王",20); }

    使用enum创建枚举类:

    public enum EnumCustom { XIAOMING("小明",18),XIAOWANG("小王",20); // 类的属性 final private final String name; private final int age; //构造器私有化 private EnumCustom(String name, int age) { this.name = name; this.age = age; } //提供公共方法获取属性 public String getName() { return name; } public int getAge() { return age; } }

    其中枚举类有两个方法:

    public void init(){ //以数组形式返回当前类型的所有对象 EnumCustom[] values = EnumCustom.values(); //参数传入要是该类型中有的对象名称 EnumCustom.valueOf("XIAOMING"); }

    当枚举类实现接口时可在声明对象时实现其接口方法也可以在类里实现共用一个

    public enum EnumCustom implements Info{ //各自实现接口方法 XIAOMING("小明",18){ public void show(){ System.out.print("我是小明"); } },XIAOWANG("小王",20){ public void show(){ System.out.print("我是小王"); } }; // 类的属性 final private final String name; private final int age; //构造器私有化 private EnumCustom(String name, int age) { this.name = name; this.age = age; } //提供公共方法获取属性 public String getName() { return name; } public int getAge() { return age; } //统一实现接口方法 public void show(){ System.out.print("我是学生"); } }

    注解(Annotation)

    从Java 5 开始Java增加了对元数据的支持也就是Annotation。

    Annotation其实就是代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取并执行相应的处理。通过使用Annotation程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。Annotation 可以向修饰符一样被使用,可用于修饰包、类、构造器、成员变量等,能被用来为程序元素设置元数据。

    使用Annotation时要在其前面加上@符号并把该Annotation当成修饰符来用,像Java中已有的Annotation @Override(用于限定重写父类方法)、@Deprecated(用于表示某个程序元素类、方法等已过时)等。 自定义注解:

    //1.新建注解类 类型修饰符用@interface 是一种特殊的接口 public @interface CustomAnnotation { //2.定义属性只不过该属性带()后面可以省略默认值; String value() default ""; String name() default "小明"; } //在需要的地方使用 @CustomAnnotation(name = "小王") public void init(){ }

    获取注解值: 在获取注解值之前我们还需要了解一个概念“元Annotation”:就是用于修饰其他Annotation的Annotation。其中常用的有@Retention、@Target、@Documented、@Inherited

    @Retention 只能修饰一个Annotation定义,用于指定该Annotation可以保留多长时间@Retention包含一个RetentionPolicy类型的成员变量使用@Retention时必须为成员变量指定值: RetentionPolicy.SOURCE:只保留在源码中编译时会自动丢弃; RetentionPolicy.ClASS:编译器将把注解记录在class文件中,运行时JVM不会保留注解也是注解类型的默认值; RetentionPolicy.RUNTIME:编译器将把注解记录在class文件中,运行时JVM会保留注解可通过反射获取该注解。@Target 用于指定被修饰的Annotation能修饰程序的那些元素,@Target中包含了一个ElementType类型的数组可用ElementType枚举类中的常量来指定@Target值如: ElementType.FIELD(表示该注解可用于修饰属性)、ElementType.METHOD(表示该注解可用于修饰方法)、ElementType.ElementType.METHOD(表示该注解可用于修饰参数)等;@Documented用于指定被@Documented修饰的Annotation类将被Javadoc工具提取成文档(定义@Documented的注解的Retention 值必须为Runtime);@Inherited被它修饰的Annotation将具有继承性如果某个类使用了被@Inherited修饰的Annotation则其子类将自动具有改注解。

    获取注解

    //定义注解使用范围 //1.新建注解类 类型修饰符用@interface 是一种特殊的接口 @Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation { //2.定义属性只不过该属性带()后面可以省略默认值; String value() default ""; String name() default "小明"; } //在需要的地方使用该注解 @CustomAnnotation(name = "小王") public class TestEntity { @CustomAnnotation(name = "小李") private String name; private String sex; public TestEntity() { } public TestEntity(String name) { this.name = name; } public String getName() { return name; } @CustomAnnotation(name = "小牛") public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } //获取该类的注解值 public void init() throws ClassNotFoundException { Class<?> aClass = Class.forName("com.li.test.TestEntity"); //获取属性注解值 Field[] declaredFields = aClass.getDeclaredFields(); for (int i = 0; i < declaredFields.length; i++) { CustomAnnotation annotation = declaredFields[i].getAnnotation(CustomAnnotation.class); if (annotation!=null) System.out.print(annotation.name()); } //获取方法的注解值 Method[] declaredMethods = aClass.getDeclaredMethods(); for (int i = 0; i < declaredMethods.length; i++) { CustomAnnotation annotation = declaredMethods[i].getAnnotation(CustomAnnotation.class); if (annotation!=null) System.out.print(annotation.name()); } //获取类的注解值 CustomAnnotation annotation = aClass.getAnnotation(CustomAnnotation.class); String name = annotation.name(); String value = annotation.value(); System.out.print(name); } //输出为 小李小牛小王

    个人微信公众号,欢迎关注及时获取技术干货!

    最新回复(0)