亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一文搞懂java反射基本API

 更新時間:2023年11月13日 11:10:55   作者:bug生產(chǎn)者  
這篇文章主要為大家介紹了一文搞懂java反射基本API,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

反射

Java提供反射來在運(yùn)行時狀態(tài)下動態(tài)的獲取類的屬性、方法等信息,在框架中很多地方都應(yīng)用到了反射

反射的操作 獲取Class對象

獲取Class對象的四種方式

// 第一種
Class<Person> clazz = Person.class;
// 第二種
Person person = new Person();
Class clazz1 =  person.getClass();
// 第三種
String className = "com.zhanghe.study.reflect.Person";
Class clazz2 = Class.forName(className);
// 第四種 使用類加載器
ClassLoader classLoader = TestClass.class.getClassLoader();
Class clazz3 = classLoader.loadClass(className);

獲取構(gòu)造器

Class clazz = TestConstructor.class;
Constructor[] constructors = clazz.getConstructors();
System.out.println("=========clazz.getConstructors()====只能獲取到公有的構(gòu)造器");
for(Constructor constructor : constructors){
  System.out.println(constructor);
}
System.out.println("*****************");

System.out.println("=========clazz.getConstructor()=====獲取公有無參構(gòu)造器");
Constructor constructor = clazz.getConstructor();
System.out.println(constructor);
System.out.println("*****************");

System.out.println("=========clazz.getDeclaredConstructors()=====獲取所有構(gòu)造器(包括私有、受保護(hù)、默認(rèn)、公有)");
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for(Constructor declaredConstructor : declaredConstructors){
  System.out.println(declaredConstructor);
}
System.out.println("*****************");

System.out.println("=========clazz.getDeclaredConstructor()====獲取指定參數(shù)類型的構(gòu)造器");
Constructor declaredConstructor = clazz.getDeclaredConstructor(String.class);
System.out.println(declaredConstructor);
System.out.println("*****************");

獲取屬性

System.out.println("======getFields===========");
// 獲取所有的屬性  只能獲取到該類和父類中public的
Field[] fields = clazz.getFields();
for (Field f : fields){
  System.out.println(f.getName());
}
System.out.println("======getDeclaredFields===========");
// 獲取所有的屬性  本類中聲明的所有的屬性都可以獲取到,不可以獲取到父類的
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f : declaredFields){
  // 獲取權(quán)限修飾符
  int i = f.getModifiers();
  String m = Modifier.toString(i);
  System.out.println("Modifier:"+m);
  // 獲取屬性類型
  Class type = f.getType();
  System.out.println("type:"+type.getTypeName());
  // 獲取屬性名
  System.out.println("name:"+f.getName());
}

獲取方法

System.out.println("======getMethods===========");
// 獲取本類和父類中所有的public方法
Method[] methods = clazz.getMethods();
for(Method m : methods){
  System.out.println(m.getName());
}

System.out.println("======getDeclaredMethods===========");
// 獲取本類中所有的方法,包括私有方法  父類的無法獲取
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
  // 獲取注解
  Annotation[] annotations = m.getAnnotations();
  // 權(quán)限修飾符
  System.out.println("Modifier:"+Modifier.toString(m.getModifiers()));
  // 返回值類型
  Class reture = m.getReturnType();
  // 形參列表
  Class[] params = m.getParameterTypes();
  // 方法名
  System.out.println(m.getName());
  // 異常類型
  Class[] exceptions = m.getExceptionTypes();
}

獲取父類信息

// 獲取父類
Class superClass = clazz.getSuperclass();

// 獲取帶泛型的父類
Type superType = clazz.getGenericSuperclass();

// 獲取父類的泛型
if(superType instanceof ParameterizedType){ // 如果存在泛型
  Type[] actualTypeArguments = ((ParameterizedType) superType).getActualTypeArguments();
}

獲取接口

// 獲取實(shí)現(xiàn)的接口
Class[] interfaces  = clazz.getInterfaces();

獲取包

// 獲取所在的包
Package aPackage = clazz.getPackage();

獲取注解

// 獲取注解
Annotation[] annotations  = clazz.getAnnotations();

反射為屬性賦值

反射執(zhí)行方法

// 通過反射獲取方法,并調(diào)用
Method method = clazz.getMethod("display");
method.invoke(person);

以上就是一文搞懂java反射基本API的詳細(xì)內(nèi)容,更多關(guān)于java反射基本API的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論