简单的反射

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


    }


}

 

posted @ 2020-09-18 14:14  妖君你好  阅读(126)  评论(0编辑  收藏  举报