
例:

JS中字符串变成了数字














方法区就是一个特殊的堆


类加载器:







获得全部构造器(图片中注释有误 )与获得指定构造器




例:
/**
* @author fengweibo
* @version 1.0
* @date 2021/5/18 18:34
*/
public class Hello {
public void sayHello(String b) {
System.out.println(b);
}
public void sayHello(int a, String b) {
System.out.println(a);
System.out.println(b);
}
public void sayHello(int a, String b, boolean c) {
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
public class Test {
public static void main(String[] args) throws Exception{
Class<Hello> h = Hello.class;
Hello hello1 = h.newInstance();
Method sayHello1 = h.getMethod("sayHello",String.class);
Method sayHello2 = h.getMethod("sayHello",int.class,String.class);
Method sayHello3 = h.getMethod("sayHello",int.class,String.class,boolean.class);
sayHello1.invoke(hello1,"hhh");
sayHello2.invoke(hello1,1,"hhh");
sayHello3.invoke(hello1,1,"hhh",true);
}
}
结果:


直接通过c1.newInstance创建对象会默认调用类的无参构造,若是没有无参构造则会报错。

通过反射获取(操作)类中的方法:

通过反射操作类中属性:



24:获取泛型方法参数类型
28:判断泛型方法参数是否是参数化类型,如果是就将他强转一下,通过29行最后一个方法获得他真实的参数信息,之后打印出来。


获得返回值类型


反射操作注解


假设数据库中存在这张student2表,就像上图。
此处的Student2方法还有get set toString类,太长了,没截完。

注解数据库名称为db_student(student2)








///// 假设数据库有一张db_student表,通过注解去生成数据库的语言,进行增删改查,自动创建表 。
https://www.bilibili.com/video/BV1p4411P7V3?p=17&spm_id_from=pageDriver
本文深入探讨Java反射机制的使用方法,包括如何通过反射创建对象、调用方法,并演示了不同构造器的应用实例。同时介绍了如何处理泛型方法参数类型及获取真实参数信息。
505

被折叠的 条评论
为什么被折叠?



