简单理解java反射技术
我理解的反射技术就是在运行时从内存中获得取得指定类的对象并得到其全部内容。
百度百科定义:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
获得class对象的三种方法:
1. 通过Class类中的静态方法forName。(通过类的完全限定名获得class对象)
2. 通过Object类中的getClass方法。(通过类的实例获得class对象)
3. 如果得到类型的话,通过.class获得。(类的.class文件名就是与其匹配的class对象)
得到了class对象,就可以根据class类中的newInstance方法按照class对象(蓝图)生成所需的对象了。
这种构建方法适用于无参的构造方法。
反射方法首先需要获得方法的对象,也就是确定由哪个对象来调度方法
然后调用class类中的getMethod方法来获得该对象中指定(由方法的签名确定)的公共成员方法。
最后由method对象进行反射。
invoke是method类中的核心方法,它的第一个参数是代表由哪个对象调用方法,以后为参数值。
作用是:对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectServiceImpl {
public void sayHello(String name){
System.err.println("Hello" + name);
}
//获得无参对象
public ReflectServiceImpl getInstance(){
ReflectServiceImpl object = null;
try {
Class c1 = Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl");
object = (ReflectServiceImpl) c1.newInstance();
Class c2 = object.getClass();
Class c3 = ReflectServiceImpl.class;
System.out.println(c3);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
//反射方法:
public Object reflectMethod(){
Object returnObj = null;
ReflectServiceImpl target = new ReflectServiceImpl();
try {
//获得制定方法
Method method = ReflectServiceImpl.class.getMethod("sayHello", String.class);
//由invoke确定由traget方法调用method的方法,并确定参数。
returnObj = method.invoke(target, "LLY");
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnObj;
}
public static void main(String[] args) {
ReflectServiceImpl rsi = new ReflectServiceImpl();
ReflectServiceImpl rsi2 = (ReflectServiceImpl) rsi.reflectMethod();
}
}
在含有参数的构建方法中可以通过
class类中的getConstructor方法设置参数的class对象
再通过newInstance反射生成对象
import java.lang.reflect.InvocationTargetException;
public class ReflectServiceImpl2 {
private String name;
public ReflectServiceImpl2(String name){
this.name = name;
}
public void sayHollo(){
System.err.println("Hello" + name);
}
//构造有参对象
public ReflectServiceImpl2 getInstance(){
ReflectServiceImpl2 object = null;
try {
object = (ReflectServiceImpl2) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
.getConstructor(String.class).newInstance("LLY");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
}