這兩個的概念在Java基礎學習中大家就學習了,但很少用到吧,他們主要是為大家學習后面的框架知識打下基礎的
注解(Annotation)
首先來說說注解的概念:
我們都知道注釋吧,就是給人看的,那注解其實意思差不多,也是一種解釋語言,并不是一種程式本身
格式:@注釋名; 可以在哪里使用:package、class、method、field相當于給他們添加一些額外的輔助資訊,后面我們可以用反射機制編程實作元資料的訪問
分類:
內置注解:
@Override 對父類方法的重寫
@Deprecated 不推薦程式員使用,但是可以使用
@SuppressWarnings 抑制編譯時的警告資訊
元注解:
@Target:用于描述注解的可以使用在什么地方
@Retention:一般都用RUNTIME表示在運行時注解(RUNTIME(運行時有效)>class(編譯之后有個class有效)>sources(原始碼級別有效))
@Document:說明該注解將包含在javadoc中
@Inherited:說明子類可以繼承父類的該注解
import java.lang.annotation.*; //測驗元注解 @MyAnnotationTest public class AnnotationTest { @MyAnnotationTest public void add(){ } } //定義一個注解 //@Target:用于描述注解的可以使用在什么地方 @Target(value = https://www.cnblogs.com/hxbhxb/archive/2021/10/03/{ElementType.METHOD,ElementType.TYPE})//表示我定義的下面的那個MyAnnotationTest注解可以使用在方法和類上面 //@Retention:一般都用RUNTIME表示在運行時注解 @Retention(value =https://www.cnblogs.com/hxbhxb/archive/2021/10/03/ RetentionPolicy.RUNTIME) //@Document:說明該注解將包含在javadoc中 @Documented //@Inherited:說明子類可以繼承父類的該注解 @Inherited @interface MyAnnotationTest{//定義一個注解前面要用@interface }
自定義注解:
直接代碼展示了:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class ZdyAnnotation { // 注解可以顯示賦值:如果沒有默認值,我們就必須要給注解賦值 @MyAnnotation(name = "金濤駭浪",array = {"成都理工","電子科大"}) public void test01(){ } // 如果只有一個值為value,可以不用寫,直接賦值即可 @MyAnnotation01("金濤海浪") public void test02(){ } } //自定義第一個注解 @Target(value =https://www.cnblogs.com/hxbhxb/archive/2021/10/03/ {ElementType.METHOD,ElementType.TYPE}) @Retention(value = RetentionPolicy.RUNTIME) @interface MyAnnotation{//定義一個注解前面要用@interface // 注解的引數:引數型別+引數名(); String name() default ""; int age() default 0; int id() default -1;//如果默認值為-1,代表不存在 String[] array(); } //自定義第二個注解 @Target(value =https://www.cnblogs.com/hxbhxb/archive/2021/10/03/ {ElementType.METHOD,ElementType.TYPE}) @Retention(value = RetentionPolicy.RUNTIME) @interface MyAnnotation01{ String value(); }
反射(Reflection)
簡單來說就是程式運行時可以改變其結構(Java本身是靜態語言,有了反射后,就變成了準動態語言,主要可以獲得注解和泛型等等)


代碼舉例:
public class FanShe { public boolean login(String username,String password){ if ("admin".equals(username)&&"123".equals(password)){ System.out.println("登陸成功"); }else { System.out.println("賬號或密碼錯誤"); } return false; } }
反射呼叫:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class FanSheTest { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { //獲取類 Class<?> aClass = Class.forName("com.sep24FanShe.FanShe"); //獲取某個特定的方法 //通過方法名+形參串列 Method m = aClass.getDeclaredMethod("login", String.class, String.class); //通過反射機制執行login方法 //用一個Object物件接收它的新的實體 Object o = aClass.newInstance(); //呼叫o物件的m方法 傳遞 admin 123引數 方法的執行結果是 Object invoke = m.invoke(o, "admin", "123");//invoke:激活的意思 System.out.println(invoke); } }
反射操作注解:
import java.lang.annotation.*; import java.lang.reflect.Field; public class ReflectionAnnotationTest { // 通過反射操作注解 public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class c1 = Class.forName("com.sep24FanShe.Person"); Annotation[] annotations = c1.getAnnotations();//獲得注解的方法 for (Annotation annotation : annotations) { System.out.println(annotation); } // 獲得注解value的值 hxbClass hxbClass = (hxbClass)c1.getAnnotation(hxbClass.class);//獲得指定注解 String value =https://www.cnblogs.com/hxbhxb/archive/2021/10/03/ hxbClass.value(); System.out.println(value); // 獲得類指定的注解 Field id = c1.getDeclaredField("id"); hxbFiled annotation = id.getAnnotation(hxbFiled.class); System.out.println(annotation.columnName()); System.out.println(annotation.type()); System.out.println(annotation.length()); } } //寫一個javaBean @hxbClass("db_person")//自定義的類名注解 class Person{ @hxbFiled(columnName = "db_id",type = "int",length = 10)//自定義的屬性注解 private int id; @hxbFiled(columnName = "db_age",type = "int",length = 10) private int age; @hxbFiled(columnName = "db_name",type = "varchar",length = 3) private String name; public Person(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } @Override public String toString() { return "Person{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person() { } } //定義一個類名注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface hxbClass{ String value(); } //定義一個屬性注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface hxbFiled{ String columnName(); String type(); int length(); }
注解和反射就先說道這里,具體后面框架的時候會用到的,那時再有什么問題再看謝謝大家;餓
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/305079.html
標籤:其他
上一篇:關于sqlmap封裝exe
