public class Reflect {
public void sayHello(){
System.out.println("===========");
}
public static void main(String[] arg){
Reflect object = null;
try {
//反射构建对象
object = (Reflect)Class.forName("com.example.reflect.Reflect").newInstance();
//调用对象方法sayHello()
object.sayHello();
//反射方法
Method method = Reflect.class.getMethod("sayHello");
Object reflect = method.invoke(object);
System.out.println(reflect);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
if(object!=null)
object.sayHello();
return;
}
}