激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java 反射機(jī)制

java 反射機(jī)制

2020-08-03 16:24fhw Java教程

本文主要介紹了java反射機(jī)制的相關(guān)知識(shí),具有一定的參考價(jià)值,下面跟著小編一起來看下吧

本文導(dǎo)引:

通過反射機(jī)制

  • 獲取類的基本信息
  • 獲取類的注解信息
  • 獲取泛型信息
java" id="highlighter_23362">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package reflection;
@AnnotationUserTable("datebaseExample")
public class User {
 @AnnotationUserField(uName="name",type="varchar",length=10)
 private String name;
 @AnnotationUserField(uName="age",type="int",length=3)
 private int age;
 @AnnotationUserField(uName="sex",type="char",length=2)
 private String sex;
 public User() {
  super();
 }
 public User(String name, int age, String sex) {
  super();
  this.name = name;
  this.age = age;
  this.sex = sex;
 }
 public String getName() {
  return name;
 }
 public void setName() {
  this.name = "test";
 }
 public int getAge() {
  return age;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
}
 
bean:User
?
1
2
3
4
5
6
7
8
9
10
11
12
package reflection;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationUserTable {
 String value();
}
 
自定義注解:類注解
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package reflection;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationUserField {
 String uName();
 String type();
 int length();
}
 
自定義注解:屬性注解
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Demo01 {
 static Class<?> c = null;
 public static void main(String[] args) {
   try {
    c = Class.forName("reflection.User");
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
  test();//獲取類的屬性、方法等信息
 }
 static void test(){
  try {
   // 獲取類的名稱
   System.out.println("獲取類的名稱");
   System.out.println("getName():" + c.getName());// 獲得包名+類名
   System.out.println("getSimpleName():" + c.getSimpleName());// 獲得類名
   System.out.println("getCanonicalName():" + c.getCanonicalName());// 獲得類名
   System.out.println("*******************************");
   // 獲取屬性信息
   System.out.println("獲取屬性信息");
   Field[] fields = c.getDeclaredFields();
   // Field[] fields = c.getFields(); 只能獲取public修飾的屬性信息
   for (Field f : fields) {
    String fName = f.getName();
    System.out.println(c.getDeclaredField(fName));
   }
   System.out.println("*******************************");
   // 獲取方法信息
   System.out.println("獲取方法信息");
   Method[] methods = c.getDeclaredMethods();
   for (Method m : methods) {
    // String mName = m.getName();
    System.out.println(m.getName() + "-->" + m);
   }
   System.out.println("通過名稱單獨(dú)獲取對應(yīng)的getName方法:" + c.getDeclaredMethod("getName"));
   System.out.println("通過名稱單獨(dú)獲取對應(yīng)的setSex方法:" + c.getDeclaredMethod("setSex", String.class));// 方法有參,必須傳遞參數(shù)類型
   System.out.println("*******************************");
   // 獲取構(gòu)造器信息
   System.out.println("獲取構(gòu)造器信息");
   Constructor<?>[] constructor = c.getConstructors();
   for (Constructor<?> cons : constructor) {
    System.out.println(cons);
   }
  } catch (NoSuchFieldException | SecurityException e) {
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   e.printStackTrace();
  }
 }
}
 
main1:通過反射機(jī)制獲取類的基本信息

output:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
獲取類的名稱
getName():reflection.User
getSimpleName():User
getCanonicalName():reflection.User
*******************************
獲取屬性信息
private java.lang.String reflection.User.name
private int reflection.User.age
private java.lang.String reflection.User.sex
*******************************
獲取方法信息
getName-->public java.lang.String reflection.User.getName()
setName-->public void reflection.User.setName()
setSex-->public void reflection.User.setSex(java.lang.String)
getSex-->public java.lang.String reflection.User.getSex()
getAge-->public int reflection.User.getAge()
通過名稱單獨(dú)獲取對應(yīng)的getName方法:public java.lang.String reflection.User.getName()
通過名稱單獨(dú)獲取對應(yīng)的setSex方法:public void reflection.User.setSex(java.lang.String)
*******************************
獲取構(gòu)造器信息
public reflection.User()
public reflection.User(java.lang.String,int,java.lang.String)
 
View Console

下面的例子,是通過反射機(jī)制獲取類的注解信息。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package reflection;
import java.lang.reflect.Field;
/**
 * 獲取類的屬性、方法等信息
 * 1.獲取元素對象(如屬性)(注意:讀取類的注解,看似要少一步)
 * 2.獲取該元素對象的指定類型的注解對象
 * 3.讀取注解對象相應(yīng)的值
 */
public class Test02 {
 static Class<?> c = null;
 public static void main(String[] args) {
   try {
    c = Class.forName("reflection.User");
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
  test();
 }
 static void test(){
  try {
   // 獲取類的指定注解
   System.out.println("***********類的指定注解**************");
   AnnotationUserTable table = (AnnotationUserTable)c.getAnnotation(AnnotationUserTable.class);
   System.out.println(table.value());
   // 獲取屬性的指定注解
   System.out.println("***********屬性的指定注解*************");
   Field field = c.getDeclaredField("name");
   AnnotationUserField annoField = (AnnotationUserField)field.getAnnotation(AnnotationUserField.class);
   System.out.println(annoField.uName()+"\t"+annoField.type()+"\t"+annoField.length());
   // 根據(jù)獲得的表名、字段的信息,拼寫出DDL語句,然后通過JDBC連接數(shù)據(jù)庫查詢
  } catch (NoSuchFieldException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  }
 }
}

output:

?
1
2
3
4
***********類的指定注解**************
datebaseExample
***********屬性的指定注解*************
name varchar 10

下面的例子,是通過反射機(jī)制獲取泛型信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package reflection;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
/**
 * 通過反射機(jī)制獲取泛型
 * @author Administrator
 *
 */
public class Test03 {
 public static void main(String[] args) {
  Class<?> c = Test03.class;
  try {
   System.out.println("*******獲取參數(shù)值的類型**********");
   Method m1 = c.getDeclaredMethod("method01", Map.class,List.class);
   Type[] types = m1.getGenericParameterTypes();
   for(Type t:types){
    System.out.println(t.getTypeName());
    System.out.println(t.toString());   
   }
   System.out.println("*******獲取返回值的類型**********");
   Method m2 = c.getDeclaredMethod("method02");
   Type ret = m2.getGenericReturnType();
   System.out.println(ret.getTypeName());
   System.out.println(ret.toString());
  } catch (NoSuchMethodException | SecurityException e) {
   e.printStackTrace();
  }
 }
 public void method01(Map<String,String> args1,List<Integer> args2){
 }
 public Map<String,String> method02(){
  return null;
 }
}
 
通過反射機(jī)制獲取泛型信息

output:

?
1
2
3
4
5
6
7
8
java.util.Map<java.lang.String, java.lang.String>
java.util.Map<java.lang.String, java.lang.String>
java.util.Map<java.lang.String, java.lang.String>
java.util.Map<java.lang.String, java.lang.String>
java.util.List<java.lang.Integer>
java.util.List<java.lang.Integer>
 
View Console

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!

原文鏈接:http://www.cnblogs.com/fhw-space/p/6367325.html#start

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久伊人精品视频 | 免费a级毛片大学生免费观看 | 中文字幕国产日韩 | 香蕉成人在线视频 | 欧美视频99| 欧洲成人一区 | 久产久精品 | 成人综合免费视频 | 污黄视频在线观看 | 娇妻被各种姿势c到高潮小说 | 欧美视频在线一区二区三区 | 亚洲乱操| 久久综合一区 | 亚洲视频在线观看免费 | 性少妇videosexfreexx入片 | 九九色精品 | 在线91视频 | 国产精品成人免费一区久久羞羞 | hdhdhd69ⅹxxx黑人 | 九九热久久免费视频 | 91av日韩| 欧美a∨一区二区三区久久黄 | 久久精品日产高清版的功能介绍 | 毛片在线免费播放 | 一级大黄毛片 | 在线看三级 | 精品国产一区二区三区成人影院 | 欧美激情精品久久久久久黑人 | 国产成人自拍视频在线 | 久久成人精品视频 | 在线观看一区二区三区四区 | 国产日韩在线观看视频 | 高清视频91 | 免费毛片观看 | 欧美一级淫片a免费播放口 九九视频精品在线 | 一区二区三区视频播放 | 91色爱| 欧美日韩在线播放 | 国产xxxx免费| 国产午夜精品久久久久婷 | 久草手机视频在线观看 |