/**
-
Class的简单使用方法
-
@author 70241
*/
public class ClassDemo {
@Test
public void classTest1() {
try {
Class class1 = Class.forName(“com.dingyu.User”);// 第一种获取Class对象的方法
User user = new User();
Class class2 = user.getClass();// 第二种获取Class对象的方法
Class class3=User.class;//第三种获取Class对象的方法
System.out.println(“接下来判断到底同一类的不同对象的Class对象是不是同一个:”
- (class1.hashCode() == class2.hashCode()&&class1.hashCode() == class3.hashCode()));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void classTest2() {
String[] s1 = new String[10];
String[] s2 = new String[30];
String[][] s3 = new String[3][30];
System.out.println(s1.getClass().hashCode()==s2.getClass().hashCode());
System.out.println(s1.getClass().hashCode()==s3.getClass().hashCode());
}
}
2.Class获取类的属性,构造器,方法和注解的简单使用
package com.dingyu;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.re
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
flect.Method;
import org.junit.Test;
/**
-
Class的简单用法
-
@author dingyu
*/
public class ClassDemo02 {
@Test
public void usingClass() throws Exception {
Class userClass = Class.forName(“com.dingyu.User”);
// 获得类名
System.out.println(userClass.getName());// 获得全类名
System.out.println(userClass.getSimpleName());// 获得类名
// 获得属性
Field[] fields = userClass.getDeclaredFields();// 获得所有的属性
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.反射获得带泛型的参数或返回值里泛型的的类型
//使用反射去修改属性的值
Field field = class1.getDeclaredField(“age”);
field.setAccessible(true);//因为age是私有的,加上这句就表示这个属性不需要做安全检查
field.set(user1, 20);
System.out.println(field.get(user1));
System.out.println(user1.getAge());
}
}