java反射技术
可以配置:类的全限定类名、方法、参数,完成对象的初始化还可以反射某些方法。
作用:增强java的可配置性。
首先创建一个类;
public class ReflectServiceImpl {
public String SayHello(String name){
System.out.println("hallo"+name);
return name;
}
}
创建一个方法获取该类的实例对象;无参的通过class.forName()的方式获取类的实例对象。forName 参数是类的全限定类名,即类的包名加类名
@param className the fully qualified name of the desired class.
public static ReflectServiceImpl getInstanceObject() {
Object obj = null;
try {
obj = Class.forName("JavaDesignModel.ReflectServiceImpl").newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
return (ReflectServiceImpl) obj;
}
public static void main(String[] args) {
ReflectServiceImpl instanceObject = getInstanceObject();
System.out.println(instanceObject);
}
结果:
JavaDesignModel.ReflectServiceImpl@14ae5a5
有参数的构造方法:注意这里, 有参数的构造方法一定是public public class
ReflectServiceImpl2 {
private String name;
//有参的构造方法
public ReflectServiceImpl2(String name) {
this.name = name;
System.out.println(name);
}
public ReflectServiceImpl2() {
}
public String SayHello(String name) {
System.out.println("hallo" + name);
return name;
}
}
getConstructor 去匹配的是public的类 Member.PUBLIC 如果该方法是私有
的返回NoSuchMethodException
/**
* Returns a {@code Constructor} object that reflects the specified
* public constructor of the class represented by this {@code Class}
* object. The {@code parameterTypes} parameter is an array of
* {@code Class} objects that identify the constructor's formal
* parameter types, in declared order.
*
* If this {@code Class} object represents an inner class
* declared in a non-static context, the formal parameter types
* include the explicit enclosing instance as the first parameter.
*
* <p> The constructor to reflect is the public constructor of the class
* represented by this {@code Class} object whose formal parameter
* types match those specified by {@code parameterTypes}.
*
* @param parameterTypes the parameter array
* @return the {@code Constructor} object of the public constructor that
* matches the specified {@code parameterTypes}
* @throws NoSuchMethodException if a matching method is not found.
* @throws SecurityException
* If a security manager, <i>s</i>, is present and
* the caller's class loader is not the same as or an
* ancestor of the class loader for the current class and
* invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to the package
* of this class.
*
* @since JDK1.1
*/
@CallerSensitive
public Constructor<T> getConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
return getConstructor0(parameterTypes, Member.PUBLIC);
}
private static ReflectServiceImpl2 getInstanceObject() {
Object obj = null;
try {
obj = Class.forName("JavaDesignModel.ReflectServiceImpl2").getConstructor(String.class).newInstance("张三");
} catch (InstantiationException | InvocationTargetException |
NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
return (ReflectServiceImpl2) obj;
}
```
public static void main(String[] args) {
ReflectServiceImpl2 instanceObject = getInstanceObject();
}
反射创建无参的对象, 反射调用方法
public class ReflectServiceImpl4 {
public void SayHello(String name) {
System.out.println("hallo" + name);
}
} public static Object getInstance(){
Object obj = null;
try {
obj = Class.forName("JavaDesignModel.ReflectServiceImpl4").newInstance();
obj.getClass().getMethod("SayHello",String.class).invoke(obj,"张三");
}catch (IllegalAccessException | InstantiationException|ClassNotFoundException |
NoSuchMethodException | InvocationTargetException e ){
e.printStackTrace();
}
return obj;
}
public static void main(String[] args) {
System.out.println(getInstance()); //调用方法输出对象
}