java反射构造对象_java反射构建对象和方法的反射调用

本文详细介绍了Java反射技术的使用,包括无参数和带参数的构造器创建对象,以及如何调用方法。通过反射,我们可以动态地获取类信息并操作对象,增强了Java的灵活性。示例代码展示了如何通过Class.forName()和newInstance()方法创建对象,以及如何使用getMethod()和invoke()调用类的方法。
摘要由CSDN通过智能技术生成

Java反射技术应用广泛,其能够配置:类的全限定名,方法和参数,完成对象的初始化,设置是反射某些方法。可以增强java的可配置性。

1.1 通过反射构建对象(无参数):

例如我们使用 ReflectServiceImpl 类讲解这个例子

1 public classReflectServiceImpl {2 public voidsayHello(String name){3 System.out.println("hello"+name);4 }5 }

我们通过反射的方法去构建它。

1 publicReflectServiceImpl getInstance(){2 ReflectServiceImpl object=null;3 try{4 object=(ReflectServiceImpl)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();5 } catch (InstantiationException | IllegalAccessException |ClassNotFoundException e) {6 e.printStackTrace();7 }8 returnobject;9 }

其中第4行:object=(ReflectServiceImpl)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();

是给类加载器注册一个类ReflectServiceImpl的权限定名,之后通过newInstance方法初始化一个类对象。

1.2 通过反射构建对象(类的构造器中带有参数):

我们使用ReflectServiceImpl2这个类去理解:

1 public classReflectServiceImpl2 {2 privateString name;3 publicReflectServiceImpl2(String name) {4 this.name=name;5 }6 public voidsayHello(String name){7 System.out.println("hello"+name);8 }9 }

此时 ReflectServiceImpl2的构造器带有参数  publicReflectServiceImpl2(String name){xxxx};

此时我们该如何利用反射生成对象呢?只需要在类加载器注册的使用getConstructor(参数)方法。其中参数是我们构造器中的参数的类型。代码如下:

1 publicReflectServiceImpl2 getInstance(){2 ReflectServiceImpl2 object=null;3 try{4 object=(ReflectServiceImpl2)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")5 .getConstructor(String.class).newInstance("张三");6 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | InvocationTargetException |NoSuchMethodException e) {7 e.printStackTrace();8 }9 returnobject;10 }

如4、5行所示:

先通过forName加载到类的加载器。然后通过getConstructor方法,它的参数可以是多个,这里定义String.class,意为有且只有一个参数类型为String 的构建方法。通过这个方法可以对重名方法进行排除,此时再用newInstance方法生成对象,只是newInstance方法也多了一个参数“张三”而已。实际上就等于object=new ReflectServiceImpl2("张三").只是利用了反射来生成对象而已。

1.3 反射方法

在使用反射方法之前需要先获取对象,得到了方法才能够去反射。

我们使用ReflectServiceImpl 类为例。

ReflectServiceImpl 类代码:

1 public classReflectServiceImpl {2 public voidsayHello(String name){3 System.out.println("hello"+name);4 }5 }

调用方法:

1 publicObject reflect(){2 ReflectServiceImpl object=null;3 Object returnObj=null;4 //反射生成对象

5 try{6 object = (ReflectServiceImpl) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();7 //反射生成方法并调度

8 Method method = object.getClass().getMethod("sayHello", String.class);9 returnObj= method.invoke(object, "张三");10 }catch( ClassNotFoundException| NoSuchMethodException| InvocationTargetException| IllegalAccessException|InstantiationException e ) {11 e.printStackTrace();12 }13 returnreturnObj;14 }

当有具体对象 object(类型为ReflectServiceImpl),而不知道具体是哪个类的时候,也可以使用object.getClass().getMethod("sayHello", String.class);来替代它,其中第一个参数是方法的名称,第二个参数是参数类型,是一个列表,多个参数可以继续编写多个类型,这样便能够获得反射的方法对象。反射方法时运用 method.invoke(object, "张三");调用的,第一个参数为object,就是确定用哪个对象调用方法,而“张三”是参数,这就等同于object.sayHello("张三");若存在多个参数可以写成Method.invoke(target,obj1,obj2.obj3...),这些要根据对象的具体方法来确定。

1.4 测试

以ReflectServiceImpl为例:

1 packagecom.lean.reflect;2 importjava.lang.reflect.InvocationTargetException;3 importjava.lang.reflect.Method;4 public classReflectServiceImpl {5 //属性

6 privateString name;7 //默认的构造方法

8 publicReflectServiceImpl() {9 super();10 }11 //带参数的构造方法

12 publicReflectServiceImpl(String name) {13 this.name=name;14 }15 //方法 sayHelo

16 public voidsayHello(String name){17 System.out.println("hello "+name);18 }19 //调用方法

20 publicObject reflect(){21 ReflectServiceImpl object=null;22 Object returnObj=null;23 //反射生成对象

24 try{25 object = (ReflectServiceImpl) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();26 //反射生成方法并调度

27 Method method = object.getClass().getMethod("sayHello", String.class);28 returnObj= method.invoke(object, "张三");29 }catch( ClassNotFoundException| NoSuchMethodException| InvocationTargetException| IllegalAccessException|InstantiationException e ) {30 e.printStackTrace();31 }32 returnreturnObj;33 }34 //获取对象

35 publicReflectServiceImpl getInstance(){36 ReflectServiceImpl object=null;37 try{38 object=(ReflectServiceImpl)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();39 } catch (InstantiationException | IllegalAccessException |ClassNotFoundException e) {40 e.printStackTrace();41 }42 returnobject;43 }44 //测试

45 public static voidmain(String[] args) {46 ReflectServiceImpl rsl= newReflectServiceImpl();47 rsl.reflect();48 }49 }

效果图:

a3c79a906a487765810255e2d3f2234c.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值