反射-Method-Java

Posted fangke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反射-Method-Java相关的知识,希望对你有一定的参考价值。

 1 import java.lang.reflect.InvocationTargetException;
 2 import java.lang.reflect.Method;
 3 
 4 /**
 5  * Created with IDEA
 6  * author:foreign
 7  * Date:2019/9/30
 8  * Time:10:40
 9  */
10 public class ReflectionFk {
11     public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
12         Class clazz = PersonFk.class;
13         //1 获取所有的public的方法
14         Method[] methods = clazz.getMethods();
15         for (Method method : methods) {
16             System.out.println("public的方法:" + method);
17         }
18         //1.1 获取指定的方法
19         Method setName = clazz.getMethod("setName", new Class[]{String.class});
20         System.out.println("获取指定的方法:" + setName);
21         //1.2 获取指定的无参数方法
22         Method getName = clazz.getMethod("getName", null);
23         System.out.println("获取无参数的方法:" + getName);
24         //2 获取方法的参数
25         Method setAge = clazz.getMethod("setAge", new Class[]{Integer.class});
26         Class<?>[] parameterTypes = setAge.getParameterTypes();
27         for (Class parameter : parameterTypes) {
28             System.out.println("获取方法参数的类型:" + parameter);
29         }
30         //3 获取方法的返回类型
31         Class<?> returnType = setAge.getReturnType();
32         System.out.println("方法的返回类型:" + returnType);
33         //4 通过method调用非静态方法
34         PersonFk personFk = new PersonFk();
35         Method setAge1 = clazz.getMethod("setAge", new Class[]{Integer.class});
36         Object invoke = setAge1.invoke(personFk, Integer.class);
37         System.out.println("调用非静态方法:" + invoke);
38         //4.1 通过method的静态方法,如果setGender为静态方法,则第一个参数传入null即可
39         Method setGender = clazz.getMethod("setGender", Boolean.class);
40         Object invoke1 = setGender.invoke(null, true);
41         System.out.println("调用静态方法:" + invoke1);
42     }
43 }

实体类

 1 /**
 2  * Created with IDEA
 3  * author:foreign
 4  * Date:2019/9/30
 5  * Time:10:39
 6  */
 7 public class PersonFk {
 8     private String name;
 9     private Integer age;
10     private Boolean gender;
11     public String desc;
12 
13     public PersonFk(String name, Integer age, Boolean gender, String desc) {
14         this.name = name;
15         this.age = age;
16         this.gender = gender;
17         this.desc = desc;
18     }
19 
20     public PersonFk() {
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setName(String name) {
28         this.name = name;
29     }
30 
31     public Integer getAge() {
32         return age;
33     }
34 
35     public void setAge(Integer age) {
36         this.age = age;
37     }
38 
39     public Boolean getGender() {
40         return gender;
41     }
42 
43     public void setGender(Boolean gender) {
44         this.gender = gender;
45     }
46 
47     public String getDesc() {
48         return desc;
49     }
50 
51     public void setDesc(String desc) {
52         this.desc = desc;
53     }
54 }

 

以上是关于反射-Method-Java的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 Ray March 片段着色器反射纹理查找会减慢我的帧速率?

OpenGL片段着色器不照亮场景

将 OpenGL 片段着色器设置为仅通过漫反射减少 vec4 色点的 RGB 值,而不是 alpha

反射机制

反射机制入门

反射机制入门