反射类及方法执行

前提:有这么一个实体类。
package com.example.test;

public Class Person
{
private int age;

public void setAge(int age)
{
this.age = age;
}

public int getAge()
{
return age;
}
}

1、 Class<?> java.lang. Class.forName( String className) throws ClassNotFoundException

Class.forName("android.view.ScaleGestureDetector");

或可以定义接收对象Class<?> cl = Class.forName("com.example.test.Person");//?不用替换。

将返回  该类  对象。

Returns a Class object which represents the class with the given name. The name should be the name of a non-primitive class, as described in theclass definition. Primitive types can not be found using this method; useint.class or Integer.TYPE instead.

If the class has not yet been loaded, it is loaded and initialized first. This is done through either the class loader of the calling class or one of its parent class loaders. It is possible that a static initializer is run as a result of this call.

Throws
ClassNotFoundExceptionif the requested class can not be found.
LinkageErrorif an error occurs during linkage
ExceptionInInitializerErrorif an exception occurs during static initialization of a class.  


2、Method java.lang.Class.getMethod(String name, Class... parameterTypes) throws NoSuchMethodExceptionSecurityException


3、Object java.lang.reflect.Method.invoke(Object receiver, Object... args) throwsIllegalAccessException,IllegalArgumentException,InvocationTargetException

Returns the result of dynamically invoking this method. Equivalent to receiver.methodName(arg1, arg2, ... , argN).

If the method is static, the receiver argument is ignored (and may be null).

If the method takes no arguments, you can pass (Object[]) null instead of allocating an empty array.

If you're calling a varargs method, you need to pass an Object[] for the varargs parameter: that conversion is usually done injavac, not the VM, and the reflection machinery does not do this for you. (It couldn't, because it would be ambiguous.)

Reflective method invocation follows the usual process for method lookup.

If an exception is thrown during the invocation it is caught and wrapped in an InvocationTargetException. This exception is then thrown.

If the invocation completes normally, the return value itself is returned. If the method is declared to return a primitive type, the return value is boxed. If the return type is void, null is returned.

Parameters
receiverthe object on which to call this method (or null for static methods)
argsthe arguments to the method
Returns
  • the result
Throws
NullPointerExceptionif receiver == null for a non-static method
IllegalAccessExceptionif this method is not accessible (see AccessibleObject)
IllegalArgumentExceptionif the number of arguments doesn't match the number of parameters, the receiver is incompatible with the declaring class, or an argument could not be unboxed or converted by a widening conversion to the corresponding parameter type
InvocationTargetExceptionif an exception was thrown by the invoked method 


Method method_setAge = cl.getMethod("setAge",new Class[]{ int.class });

method_setAge.invoke(new Person(),  { new Integer(25) });


Method method_getAge = cl.getMethod("getAge", null);

Integer age = method_getAge.invoke(new Person(),  null);


上面用到类的实例化对象时,用new Person()。另外还可以用class.newInstance();和constructor.newInstance();

关于这两者区别:

1、

尝试:通过Class.NewInstance()调用私有构造函数:

结果:通过Class.NewInstance()调用私有构造函数【失败】


尝试:通过Constructor.newInstance()调用私有构造函数:

结果:A's constructor is called.


2、class.newInstance调用无参构造。

constructor.newInstance()可调用含参构造。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值