Annotation 学习

it2023-12-04  79

package com.koyw23.Annotation;

import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface Ann { int text(); String textStr();}

 

 

package com.koyw23.Annotation;

@Ann(text = 2, textStr = "name")public class AnnClass {

private int id; private int Ann_Name;

public int getId() { return id; }

public void setId(int id) { this.id = id; }

public int getAnn_Name() { return Ann_Name; }

public void setAnn_Name(int ann_Name) { Ann_Name = ann_Name; }

public static void main(String[] args) {

try { Class<?> clazz = Class.forName("com.koyw23.Annotation.AnnClass"); Ann ann = (Ann) clazz.getAnnotation(Ann.class); boolean b = clazz.isAnnotationPresent(Ann.class); if (b) {

System.out.println(ann.text()); System.out.println(ann.textStr()); if(ann.textStr().equals("name")){ System.err.println("nishabi"); } }

} catch (ClassNotFoundException e) { e.printStackTrace(); }

}

}

转载于:https://www.cnblogs.com/koyw23/p/5669261.html

相关资源:数据结构—成绩单生成器
最新回复(0)