首页 > 编程知识 正文

反射三种获取class对象,java反射获取方法

时间:2023-05-03 07:19:06 阅读:25728 作者:1755

就是调用类中的方法。 最简单的用法是可以参数化方法。 invoke(class,method );

方法class.get method (string name,class? parameterTypes的作用是获取对象声明的发布方法

方法的第一个参数name获取方法的名称,第二个参数parameterTypes按声明顺序标识方法的形状参数类型。

person.getClass ().getmethod )、null );

获取person对象的Speak方法。 parameterTypes为空,因为Speak方法没有参数

person.getClass ().getmethod )、String.class );

获取人员对象的run方法。 由于run方法的参数是String类型,因此parameterTypes是String.class

如果对象中方法的形状参数为int类型,则parameterTypes为int.class

//getMethod的第一个参数是方法名称,第二个参数是方法的参数类型。

//因为同方法名可能是不同的参数,所以通过同时指定方法名称和参数类型来确定唯一的方法

方法方法=XXX.getclass ().getmethod )方法名称,newClass[0] );

//第一个参数是具体调用方法的对象

//第二个参数是运行此方法的具体参数

例如函数inttest(inta,String str );

对应的getMethod方法:

1.getmethod('test ',int.class,String.class );

2.getmethod('test ',new Class[]{int.class,String.class} );

//Method类的invoke(objectobj,Object args[] )方法接收的参数必须是对象,返回值始终是对象。

//如果参数是基本类型数据,则需要将其转换为适当的包装类型对象。

Publicobjectinvoke(objectobj,Object.args ) throws IllegalAccessException,IllegalArgumentException,invocationtationtargeteteted

公共类invoke obj {

公共void show () }

System.out.println (无引用show )方法);

}

公共语音显示(字符串名称) {

system.out.println(show方法: name

}

公共字符串[ ] array show {

返回警报;

}

publicstringstringshow (字符串str )。

返回str;

}

publicintintshow(intsum ) {

返回和;

}

}

MethodInvokeTest.java:

import Java.lang.reflect.invocationtargetexception;

import java.lang.reflect.Method;

公共类方法学

公共静态语音识别

main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        
        Class<InvokeObj> clazz=InvokeObj.class;
        Method[] methods=clazz.getMethods();
        //输出了Class类型的所有方法。
        System.out.println("以下输出InvokeObj类的方法");
        for(Method method:methods){
            System.out.println(method);
        }
        System.out.println();
        
         System.out.println("InvokeObj类的无参show()方法:");
         Method method1=clazz.getMethod("show",null);
        //会执行无参show()方法
         Object obj=method1.invoke(new InvokeObj(),null);
         System.out.print("输出无参show()方法的返回值:"+obj);
         System.out.println();System.out.println();
        
         System.out.println("InvokeObj类的show()方法: ");  
         Method method2 = clazz.getMethod("show", String.class);
         Object obj1 = method2.invoke(new InvokeObj(), "hello,world");  
           // System.out.println("输出有参show()方法: ");
         System.out.println();
        
         System.out.println("InvokeObj类的arrayShow()方法: ");  
         Method method3 = clazz.getMethod("arrayShow", String[].class);  
            String[] strs = new String[]{"hello", "world", "!"};  
            //数组类型的参数必须包含在new Object[]{}中,否则会报IllegalArgumentException  
            String[] strings = (String[]) method3.invoke(new InvokeObj(), new Object[]{strs});  
            for (String str : strings) {  
                System.out.println("arrayShow的数组元素:" + str);  
            }
         System.out.println();   
        
         System.out.println("InvokeObj类的StringShow()方法: ");  
            Method method4 = clazz.getMethod("stringShow", String.class);  
            String string = (String) method4.invoke(new InvokeObj(), "Thinking in java");  
            System.out.println("stringShow()方法的返回值: " + string);  
            System.out.println();
            
            System.out.println("InvokeObj类的intShow()方法: ");  
            Method method5 = clazz.getMethod("intShow", int.class);  
            int num = (Integer) method5.invoke(new InvokeObj(), 89);  
            System.out.println("intShow()方法的返回值: " + num);     
            
        
    }

}

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。