获取Class 实例方式
//方式1
// Class<Person> clazz = Person.class;
Class clazz = Person.class;
System.out.println(clazz);//class Day4_12.Person
//方式2 ,通过运行时类的对象
Person p = new Person();
Class clzz2 = p.getClass();
System.out.println(clzz2);
//方式3 通过调用Class静态方法
Class clzz3 = Class.forName("Day4_12.Person");
System.out.println(clzz3);
加载配置文件两种方法
//加载配置文件
Properties pro = new Properties();
//加载方式一: //默认路径是:当前的modul下
/*FileInputStream fis = new FileInputStream("jdbc.properties");
pro.load(fis);*/
//加载方式二:默认路径再module路径下 src中
ClassLoader classLoader = TestReflectionDemo1.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("Day4_14\\jdbc.properties");
pro.load(is);
String name = pro.getProperty("name");
String password = pro.getProperty("password");
System.out.println("name:" +name +", password: "+password);
}
通过Class 实例,创建运行时类的对象:
1.通常使用clazz.newInstance();方法创建运行时类对象。
2.要求2个先决条件
1.运行时类必须提供一个空参构造器;
2.并且空参构造器的访问权限足够。通常为public
提示:newInstance(); 创建对象,同样是需要调用构造器来完成
//实例化Class
Class clzz = Person.class;
Object obj = clzz.newInstance();
Person p = (Person) obj;
System.out.println(p);//Person{name='null', age=0}
补充说明:
在javabean中要求提供一个public 空参构造器的原因:
1.便于反射创建运行时类对象
2.便于子类继承运行时类时,默认调用super()时,保证父类有空参构造器;
获取运行时类的属性
Class<Person> clazz = Person.class;
//获取当前运行时类及其父类所有public权限的属性
Field[] fields = clazz.getFields();
//获取当前运行时类,所有的属性
Field[] dfs = clazz.getDeclaredFields();
for (Field f : dfs) {
//获取权限修饰符
int modifiers = f.getModifiers();
System.out.println(Modifier.toString(modifiers));
//获取数据类型
Class<?> type = f.getType();
System.out.println(type);
//获取变量名
String name = f.getName();
System.out.println(name);
获取运行时类的方法:
Class clazz = Person.class;
//获取运行时类及其父类的所有public方法
Method[] methods = clazz.getMethods();
//获取运行时类的所有的方法
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods) {
System.out.println(m.toString());
}
了解
//获取运行时类及其父类的所有public的构造器
Constructor[] constructors = clazz.getConstructors();
//获取运行时类的所有构造器
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
//获取运行时类的父类
Class superclass = clazz.getSuperclass();
//获取运行时类带泛型的父类
Type genericSuperclass = clazz.getGenericSuperclass();
//获取运行时类带泛型父类的泛型
Type genericSuperclass1 = clazz.getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
//获取运行时类的接口
Class[] interfaces = clazz.getInterfaces();
//获取运行时类的包
Package aPackage = clazz.getPackage();
//获取运行时类的注解
Annotation[] annotations = clazz.getAnnotations();
调用运行时类指定属性,方法,构造器
掌握:操作运行时类步骤
Class clazz = Person.class;
//1.创建运行时类的对象,h=
Person p = (Person) clazz.newInstance();
/* //获取指定属性 id属性为public public int id;
//通常不使用这种方法
Field id = clazz.getField("id");*/
//常用方式
//2,获取私有权限修饰符 private String name;
Field name = clazz.getDeclaredField("name");
//3.设置属性为可访问的
name.setAccessible(true);
//4.属性操作 设置值,指明对象,数据
name.set(p, "关云长");
//获取数据,指明哪个对象的属性值
String str = (String) name.get(p);
System.out.println(str);
掌握:调用运行时类指定方法
Class clazz = Person.class;
//1.获取运行时类对象
Person p = (Person) clazz.newInstance();
//2.获取运行时类指定的方法 public int show(String string,int num)
Method show = clazz.getDeclaredMethod("show", String.class,int.class);
//3.设置该方法时可访问的
show.setAccessible(true);
// 3.调用 invoke();参数1:指明调用的对象
Object un = show.invoke(p, "UN", 556);
int num = (int) un;
System.out.println(num);
System.out.println("----------静态方法调用----------");
//public static void run() {
//1.获取静态方法
Method run = clazz.getDeclaredMethod("run");
//2.设置访问许可
run.setAccessible(true);
//3.调用 静态方法调用对象时类
Object invoke = run.invoke(Person.class);
System.out.println(invoke);//如果方法没有返回值,invoke()返回值类为null;