JAVA#反射总结一 札记

 @Test//设置属性,调用方法及含参方法
    public void Test1() throws Exception{
        Class kb=Kobeman.class;
        Kobeman kb_1=(Kobeman) kb.newInstance();
        Field f1=kb.getField("Name");//属性
        f1.set(kb_1, "KobeSong");
        System.out.println(kb_1);
        Method m1=kb.getMethod("show");//方法
        m1.invoke(kb_1);
        Method m2=kb.getMethod("display", String.class);//含参方法
        m2.invoke(kb_1,"火星");


    }
    @Test//获取class类的实例
    public void Test2() throws Exception{
        Kobeman k=new Kobeman();
        Class k_1=k.getClass();//通过运行时类对象,调用getClass方法,返回运行时类;
        System.out.println(k_1.getName());

        Class c1=Kobeman.class;
        System.out.println(c1.getName());

        String string="SnowCH20190110.Kobeman";//文件位置
        Class c3=Class.forName(string);
        System.out.println(c3.getName());

        ClassLoader cl=this.getClass().getClassLoader();//类加载器
        Class c4=cl.loadClass(string);
        System.out.println(c4.getName());
    }
    @Test
    public void Test3() throws IOException {
        ClassLoader cl=this.getClass().getClassLoader();
        InputStream is=cl.getResourceAsStream("SnowCH20190110\\jdbc.properties");
        Properties pp=new Properties();
        pp.load(is);
        String name=pp.getProperty("user");
        String password=pp.getProperty("password");
        System.out.println(name);
        System.out.println(password);
    }
    @Test//调用Kobeman里空参构造器
    public void Test4() throws Exception{
        String string="SnowCH20190110.Kobeman";//文件位置
        Class c3=Class.forName(string);
        Object o=c3.newInstance();//调用Kobeman里空参构造器
        Kobeman k=(Kobeman)o;
        System.out.println(k);
    }
    @Test//获取构造器
    public void Test5() throws ClassNotFoundException{
        String className="SnowCH20190110.Kobeman";
        Class cl=Class.forName(className);
        Constructor[] constructor=cl.getDeclaredConstructors();//构造器
        for(Constructor c:constructor){//遍历所有构造器
            System.out.println(c);
        }
    }
    @Test//运行时类的父类
    public void Test6(){
        Class c1_2=Kobeman.class;
        Class supercl= c1_2.getSuperclass();//运行时类的父类
        System.out.println(supercl);
    }
    @Test//获取带泛型的父类
    public void Test7(){
        Class c1_3=Kobeman.class;
        Type type= c1_3.getGenericSuperclass();//带泛型的父类
        System.out.println(type);
    }




    //获取父类的泛型__重要
    @Test
    public void Test8_Important(){
        Class c1_4=Kobeman.class;
        Type type1= c1_4.getGenericSuperclass();
        ParameterizedType parameter=(ParameterizedType)type1;
        Type[] args=parameter.getActualTypeArguments();
        System.out.println(((Class)args[0]).getName());
    }





    @Test//获取实现的接口,所在包,注解
    public void Test9(){
        Class cl_5=Kobeman.class;
        Class[] interfaces=cl_5.getInterfaces();//获取实现的接口
        for(Class c:interfaces){
           System.out.println(c);
       }
        Package pack=cl_5.getPackage();//获取所在包
        System.out.println(pack);
        Annotation[] annotation=cl_5.getAnnotations();//获取注解
        for(Annotation a:annotation){
            System.out.println(a);
        }

    }

}

猜你喜欢

转载自blog.csdn.net/Iverson941112/article/details/86428761