1.通过无参构造方法实例化对象
无参构造方法,类
public class ReflectServiceImpl {
public ReflectServiceImpl() {
}
public void sayHello(String name) {
System.out.println("hello" + name);
}
}
反射调用方式
public static void main(String[] args) {
ReflectServiceImpl reflectServiceImpl = null;
try {
reflectServiceImpl = (ReflectServiceImpl) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reflectServiceImpl.sayHello("张三");
}
2. 有参构造函数,对象实例化
有参构造函数类
public class ReflectServiceImpl2 {
private int age;
private String name;
public ReflectServiceImpl2(String name, Integer age) {
this.name = name;
this.age = age;
}
public void sayHello() {
System.out.println("hello" + name + ",年龄" + age);
}
}
反射调用方式
public static void main(String[] args) {
ReflectServiceImpl2 reflectServiceImpl = null;
try {
reflectServiceImpl = (ReflectServiceImpl2) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
.getConstructor(String.class, Integer.class)
.newInstance("张三", 13);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} 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();
}
reflectServiceImpl.sayHello();
}
主要通过getConstructor方法指定构造方法参数的类型和个数。注意通过反射调用构造时,构造函数中的参数必须是类,所以参数不能是基本类型?
3. 通过方法名称,调用类中的方法
public class ReflectServiceImpl2 {
private int age;
private String name;
public ReflectServiceImpl2(String name, Integer age) {
this.name = name;
this.age = age;
}
public void sayHello() {
System.out.println("hello" + name + ",年龄" + age);
}
public void sayHello(String name, Integer age) {
System.out.println("hello" + name + ",年龄" + age);
}
}
调用无参的sayHello
public static void main(String[] args) {
ReflectServiceImpl2 reflectServiceImpl = null;
try {
reflectServiceImpl = (ReflectServiceImpl2) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
.getConstructor(String.class, Integer.class).newInstance("张三", 13);
Method method = reflectServiceImpl.getClass().getMethod("sayHello", null);
method.invoke(reflectServiceImpl, null);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} 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();
}
}
getMethod 和调用方法invoke参数都传入null。
调用两个参数的sayHello
public static void main(String[] args) {
ReflectServiceImpl2 reflectServiceImpl = null;
try {
reflectServiceImpl = (ReflectServiceImpl2) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
.getConstructor(String.class, Integer.class).newInstance("张三", 13);
Method method = reflectServiceImpl.getClass().getMethod("sayHello", String.class, Integer.class);
method.invoke(reflectServiceImpl, "李四", 16);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} 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();
}
getMethod 和调用方法invoke参数都传入参数对应的类。