The program has Notes and Annotations
* Note: For developers .
* Note: to the computer to see .
Annotations Use: The Learning Framework supports annotation development .
-
- " The notes provided by the JDK "
@Override : Describes the override of the method .
@SuppressWarnings : Suppress the warning .
@Deprecated : Markup is obsolete .
define a class : Class
define an excuse : interface
define an enumeration : enum
define an annotation : @interface
Usage:
@Interfacemyanno1{} Annotated with attributes: @Interfacemyanno2{intA ()default1; String b (); //the type of the annotation property: the base data type, the string type String,class, the annotation type, the enumeration type, and the one-dimensional array of the above types. //Date D ();Class clazz (); MyAnno3 m3 (); //AnnotationsColor c ();//Enumerationstring[] Arrs (); } @MyAnno4 ("AAA")//If the property name is value then value can be omitted (only if the property of this value is present). Public classAnnotationDemo3 {}@Interfacemyanno4{String value (); intA ()default1;}
"Custom Annotation Cases"
Annotation class
1 Packagecom.xujingyang.annotation;2 3 ImportJava.lang.annotation.ElementType;4 Importjava.lang.annotation.Retention;5 ImportJava.lang.annotation.RetentionPolicy;6 ImportJava.lang.annotation.Target;7 8 //keep to run time9 @Retention (retentionpolicy.runtime)Ten //can only act on the method One @Target (Elementtype.method) A Public@InterfaceMyTest { - -}
Test Note class
1 Packagecom.xujingyang.annotation;2 3 Public classTest {4 @MyTest5 Public voidF1 () {6System.out.println ("F1 method executed ~ ~ ~ ~ ~");7 }8 9 Public voidF2 () {TenSystem.out.println ("F2 method executed ~ ~ ~ ~ ~"); One } A - @MyTest - Public voidF3 () { theSystem.out.println ("F3 method executed ~ ~ ~ ~ ~"); - } -}
Main test class
1 Packagecom.xujingyang.annotation;2 3 ImportJava.lang.reflect.Method;4 5 Public classMain {6 Public Static voidMain (string[] args)throwsexception{7Class Clazz=test.class;8 9Method[] Methods =clazz.getmethods ();Ten for(Method method:methods) { One //Method.invoke (Clazz.newinstance ()); A //System.out.println (Method.getname ()); - Booleanb = Method.isannotationpresent (MyTest.class);//determine if you have added this annotation - if(b) { the Method.invoke (Clazz.newinstance ()); - } - } - } +}
Print results
Case two, using annotations to load parameters to get JDBC connections
Annotation class
1 PackageCom.xujingyang.jdbc;2 3 ImportJava.lang.annotation.ElementType;4 Importjava.lang.annotation.Retention;5 ImportJava.lang.annotation.RetentionPolicy;6 ImportJava.lang.annotation.Target;7 //keep to run time8 @Retention (retentionpolicy.runtime)9 //This annotation can only be added on a methodTen @Target (Elementtype.method) One Public@InterfaceJdbcinfo { A //defines several properties for loading a database, which can be assigned defaults with the default keyword -String Driverclass ()default"Com.mysql.jdbc.Driver"; - String Url (); theString User ()default"Root"; -String PWD ()default"Root"; -}
Test class
1 PackageCom.xujingyang.jdbc;2 3 ImportJava.lang.reflect.Method;4 Importjava.sql.Connection;5 ImportJava.sql.DriverManager;6 7 Public classConn {8 Public Static voidMain (string[] args)throwsException {9System.out.println (getconnection ());//Print connection address, indicating successTen } One A@JDBCInfo (url= "JDBC:MYSQL://LOCALHOST:3306/DAY16") - Public StaticConnection getconnection ()throwsexception{ - //to load a byte-code object for a class theClass<conn> Clazz=conn.class; - - //Get This method -method = Clazz.getmethod ("getconnection"); + - //Get Annotation Object +Jdbcinfo info = method.getannotation (jdbcinfo.class); A at //get the properties of each assigned value -String Driverclass =info. Driverclass (); -String URL =info. Url (); -String user =info. User (); -String pwd =info. PWD (); - in //Registration Driver - Class.forName (driverclass); to + //Get Connected - returndrivermanager.getconnection (URL, user, pwd); the } *}
Print results
The simple use of annotations is so much to remember that more in-depth research will be done later
Java meta Annotations, simple cases