简单的反射
public class ReflectService {
public void sayHi(String name){
System.out.println("hello"+ name);
}
public static void main(String[] args) throws Exception{
//反射对象
Class<?> clazz = Class.forName("com.fydemo.demo.reflectdemo.ReflectService");
ReflectService reflectService = (ReflectService)clazz.newInstance();
reflectService.sayHi("jj"); //hellojj
//反射方法
Object clazzService = Class.forName("com.fydemo.demo.reflectdemo.ReflectService").newInstance();
Method sayHiMethod = clazzService.getClass().getMethod("sayHi", String.class);
sayHiMethod.invoke(clazzService,"yy"); //helloyy
}
}