Package Annotation;import Java.lang.annotation.retention;import java.lang.annotation.retentionpolicy;// runtime indicates that the runtime person annotation also exists//@Retention (Retentionpolicy.class) @Retention (retentionpolicy.runtime) @interface person { String name (); int age ();} @Person (name= "Tom", Age=20) class test1{} @Person (name= "Jack", age=22) class test2{}//@Person (name= "Anna", age=18) Class Test3{}public class Annotationtest{public static void Printclassinto (Class C) {System.out.print (C.getname () + "). "); Person person = (person) c.getannotation (Person.class), if (person!=null) {System.out.println ("Person:" +person.name () + ", Age:" +person.age ());} else {System.out.println ("unknown Person");}} public static void Main (string[] args) {Printclassinto (Test1.class);p Rintclassinto (test2.class);p Rintclassinto ( Test3.class);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Definitions of Java Custom annotations