for (Field field : fields) {
System.out.println(field.getName());
}
System.out.println(userClass.getDeclaredField("id").getName());// 获得指定的属性
// 获得方法
Method[] methods = userClass.getDeclaredMethods();// 获得所有的方法
for (Method method : methods) {
System.out.println(method.getName());
}
Method method = userClass.getDeclaredMethod("setId", int.class);// 获得指定的方法,前面方法名,后面方法的参数
System.out.println(method.getName());
// 获得构造器
Constructor[] constructors = userClass.getDeclaredConstructors();
System.out.println(constructors.length);
Constructor constructor = userClass.getDeclaredConstructor(int.class, String.class, int.class);// 获得指定的构造器,需要指定构造的参数
System.out.println(constructor.getName());
// 获得注解
Annotation[] annotations = userClass.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
// 指定注解名
MyAnnotation annotation = (MyAnnotation)userClass.getDeclaredAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
### 3.Class动态的调用构造器,方法,修改属性
package com.dingyu;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
/**
-
使用反射动态的调用构造器,方法,修改属性
-
@author 70241
*/
public class ClassDemo03 {
@Test
@SuppressWarnings("all")
public void usingClass() throws Exception {
Class class1 = Class.forName("com.dingyu.User");
//使用反射去调用构造器
User user1 = (User) class1.newInstance();//调用的是无参的
Constructor constructor = class1.getDeclaredConstructor(int.class,String.class,int.class);//获得有参的构造器
User user2 = (User) constructor.newInstance(04,"dingyu",20);//动态生成对象
//使用反射去调用方法
Method methodSetId = class1.getDeclaredMethod("setId",int.class);
methodSetId.invoke(user1, 02);//执行user1中的setId,后面是给的参数
System.out.println(user1.getId());
//使用反射去修改属性的值
Field field = class1.getDeclaredField("age");
field.setAccessible(true);//因为age是私有的,加上这句就表示这个属性不需要做安全检查
field.set(user1, 20);
System.out.println(field.get(user1));
System.out.println(user1.getAge());
}
}
### 4.反射获得带泛型的参数或返回值里泛型的的类型
package com.dingyu;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
/**
-
反射获得带泛型的参数或返回值里泛型的的类型
-
@author dingyu
*/
public class ClassDemo04 {
public void test01(Map<Integer, String> map, String s) {
}
public Map<Integer, String> test02() {
return null;
}
public static void main(String[] args) throws Exception {
//参数中带泛型的
Method method = ClassDemo04.class.getDeclaredMethod("test01", Map.class, String.class);
Type[] types = method.getGenericParameterTypes();// 返回一个 Type对象的数组, Type以声明顺序表示由该对象表示的可执行文件的形式参数类型
// 打印这些参数的类型
for (Type type : types) {
System.out.println(type.getTypeName());
if (type instanceof ParameterizedType) {// 如果是泛型的参数
Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();// 获得泛型的的类型
for (Type type2 : actualTypeArguments) {
System.out.println(type2.getTypeName());
}
}
}
//返回值中带泛型的
Method method02 = ClassDemo04.class.getDeclaredMethod("test02");
Type type = method02.getGenericReturnType();// 返回的类型
// 打印这些返回的类型
System.out.println(type.getTypeName());
if (type instanceof ParameterizedType) {// 如果是泛型的参数
Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();// 获得泛型的的类型
for (Type type2 : actualTypeArguments) {
System.out.println(type2.getTypeName());
}
}
}
}
## 总结
总的来说,面试是有套路的,一面基础,二面架构,三面个人。
**最后,小编这里收集整理了一些资料,其中包括面试题(含答案)、书籍、视频等。希望也能帮助想进大厂的朋友,[点击这里即可免费获取](https://gitee.com/vip204888/java-p7)**
![三面蚂蚁金服成功拿到offer后,他说他累了](https://img-blog.csdnimg.cn/img_convert/6e1de72e700d50187bb448d5b996b074.png)
![三面蚂蚁金服成功拿到offer后,他说他累了](https://img-blog.csdnimg.cn/img_convert/fb11aa8acc3b5a75becb9ca8ce014dda.png)
![三面蚂蚁金服成功拿到offer后,他说他累了](https://img-blog.csdnimg.cn/img_convert/d560c2e5e5732a8d295091f95a0b8e5a.png)
总的来说,面试是有套路的,一面基础,二面架构,三面个人。
**最后,小编这里收集整理了一些资料,其中包括面试题(含答案)、书籍、视频等。希望也能帮助想进大厂的朋友,[点击这里即可免费获取](https://gitee.com/vip204888/java-p7)**
[外链图片转存中...(img-8yW637Jh-1628076789225)]
[外链图片转存中...(img-KH2iMTZE-1628076789228)]
[外链图片转存中...(img-9ELUINqD-1628076789229)]