反射类及方法执行
来源:互联网 发布:flash mac破解版下载 编辑:程序博客网 时间:2024/05/16 11:36
前提:有这么一个实体类。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
orInteger.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
2、Method java.lang.Class.getMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException
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
Returns
- the result
Throws
receiver == null
for a non-static methodAccessibleObject
)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()可调用含参构造。
- 反射类及方法执行
- 反射执行类的方法
- 反射类私有方法并执行
- JAVA 反射方式执行类方法
- 通过反射执行方法
- 黑马学习--------------------反射(执行方法)
- 通过反射执行私有方法
- 反射执行带泛型的方法
- 反射加载dll 执行方法
- 使用类的反射机制执行类中的方法
- 黑马学习---------------反射(执行其他类的main方法)
- 黑马程序员-Java反射--用反射方式执行某个类中的main方法
- Java SE__通过反射创建类并通过反射执行实例方法
- C# 动态编译及反射执行
- 用反射执行内部类的方法
- 反射中方法的获取和执行
- 反射:动态执行一个对象的方法
- php反射方式调用方法执行
- #include是什么
- Linux系统各个目录的一般作用
- make时 No rule to make target错误解决办法
- 中缀式转后缀式
- DECODE的语法
- 反射类及方法执行
- UVA 10534- Wavio Sequence
- 解决spring+iframe时 iframe src的路径问题,由不安全的根目录到WEB-INF 的安全目录
- HDOJ 2049 不容易系列之(4)——考新郎
- lua 获得系统时间
- UVA 348 Optimal Array Multiplication Sequence (区间DP)
- 互联网思维体系
- CentOS 7 安装后没有ifconfig命令
- NYOJ 330 一个简单的数学题