欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 反射

程序员文章站 2022-05-18 18:54:28
反射(Reflection): 在多个类没有公共特性时, 可以在一个基类方法中, 通过反射, 实现对这些没有公共特性的类的普遍性调用。 一:class.getMethod() ①//getMethod(String name, Class... parameterTypes) Method m ......

反射(reflection): 在多个类没有公共特性时,

                    可以在一个基类方法中,

                    通过反射,

                    实现对这些没有公共特性的类的普遍性调用。

 

一:class.getmethod()

  ①//getmethod(string name, class<?>... parametertypes)

         method method = class.getmethod("要调用的方法名称","要调用的方法的形参的类型的class");

    ②//invoke(object obj, object... args)

    method.invoke("对象实例","要调用的方法的形参");

 1 public class reflectivetest {
 2 
 3     public static void reflectivemethod(object object) {
 4 
 5         system.out.println("-------------反射使用方法--------------");
 6 
 7         class<?> clazz = object.getclass();
 8         try {
 9 
10             //getmethod():第一个参数方法名称;第二个参数:方法的形参的class
11             method sitmethod = clazz.getmethod("sit",null);
12             sitmethod.invoke(object,null);
13         } catch (nosuchmethodexception | illegalaccessexception | invocationtargetexception e) {
14             e.printstacktrace();
15         }
16 
17 
18         try {
19             method eatmethod = clazz.getmethod("eat",string.class);
20             eatmethod.invoke(object,"pork");
21         } catch (nosuchmethodexception | illegalaccessexception | invocationtargetexception e) {
22             e.printstacktrace();
23         }
24 
25         try {
26 
27             method calculatemethod = clazz.getmethod("calculate",int.class,int.class);
28             int a = (integer) calculatemethod.invoke(object,1024,2048);
29             system.out.println("calculatemethod 结果=" + a);
30         } catch (nosuchmethodexception | illegalaccessexception | invocationtargetexception e) {
31             e.printstacktrace();
32         }
33 
34     }
35 
36 
37     public static void main(string[] args) {
38 
39         dog dog = new dog();
40         robot robot = new robot();
41 
42         reflectivemethod(dog);
43         reflectivemethod(robot);
44     }
45 
46 
47 }

运行结果:

 1 -----------------反射使用方法-------------------
 2 dog sits 
 3 dog eats pork
 4 java.lang.nosuchmethodexception: com.wing.test.vo.dog.calculate(int, int)
 5     at java.lang.class.getmethod(class.java:1786)
 6     at com.wing.test.reflectivetest.reflectivemethod(reflectivetest.java:41)
 7     at com.wing.test.reflectivetest.main(reflectivetest.java:56)
 8 java.lang.nosuchmethodexception: com.wing.test.vo.robot.eat(java.lang.string)
 9     at java.lang.class.getmethod(class.java:1786)
10     at com.wing.test.reflectivetest.reflectivemethod(reflectivetest.java:33)
11     at com.wing.test.reflectivetest.main(reflectivetest.java:57)
12 -----------------反射使用方法-------------------
13 robot sits
14 robot calculates a + b = 3072
15 calculatemethod 结果=3072

 

辅助类:

 1 public class dog {
 2 
 3     public dog() {
 4     }
 5 
 6     public void sit(){
 7         system.out.println("dog sits ");
 8     }
 9 
10     public void eat(string meat){
11         system.out.println("dog eats " + meat);
12     }
13 
14 
15 }
16 
17 
18 public class robot {
19 
20     public robot() {
21     }
22 
23     public void sit(){
24         system.out.println("robot sits");
25     }
26 
27     public int calculate(int a,int b){
28         int c = a + b;
29         system.out.println("robot calculates a + b = " + c);
30         return c;
31     }
32 
33 }