JAVA学习--反射方法操作

 1     //1.获取运行时类的方法
 2     
 3     @Test
 4     public void test1(){
 5         Class clazz = Person.class;
 6         //1.getMethods():获取运行时类及其父类中所有的声明为public的方法
 7         Method[] m1 = clazz.getMethods();
 8         for(Method m : m1){
 9             System.out.println(m);
10         }
11         System.out.println();
12         
13         //2.getDeclaredMethods():获取运行时类本身声明的所有的方法
14         Method[] m2 = clazz.getDeclaredMethods();
15         for(Method m : m2){
16             System.out.println(m);
17         }
18     }
 1 //注解 权限修饰符 返回值类型 方法名 形参列表 异常
 2     @Test
 3     public void test2(){
 4         Class clazz = Person.class;
 5         
 6         Method[] m2 = clazz.getDeclaredMethods();
 7         for(Method m : m2){
 8             //1.注解
 9             Annotation[] ann = m.getAnnotations();
10             for(Annotation a : ann){
11                 System.out.println(a);
12             }
13             
14             //2.权限修饰符
15             String str = Modifier.toString(m.getModifiers());
16             System.out.print(str + " ");
17             //3.返回值类型
18             Class returnType = m.getReturnType();
19             System.out.print(returnType.getName() + " ");
20             //4.方法名
21             System.out.print(m.getName() + " ");
22             
23             //5.形参列表
24             System.out.print("(");
25             Class[] params = m.getParameterTypes();
26             for(int i = 0;i < params.length;i++){
27                 System.out.print(params[i].getName() + " args-" + i + " ");
28             }
29             System.out.print(")");
30             
31             //6.异常类型
32             Class[] exps = m.getExceptionTypes();
33             if(exps.length != 0){
34                 System.out.print("throws ");
35             }
36             for(int i = 0;i < exps.length;i++){
37                 System.out.print(exps[i].getName() + " ");
38             }
39             System.out.println();
40         }
41     }
 1 //调用运行时类中指定的方法
 2     @Test
 3     public void test3() throws Exception{
 4         Class clazz = Person.class;
 5         //getMethod(String methodName,Class ... params):获取运行时类中声明为public的指定的方法
 6         Method m1 = clazz.getMethod("show");
 7         Person p = (Person)clazz.newInstance();
 8         //调用指定的方法:Object invoke(Object obj,Object ... obj)
 9         Object returnVal = m1.invoke(p);//我是一个人
10         System.out.println(returnVal);//null
11         
12         Method m2 = clazz.getMethod("toString");
13         Object returnVal1 = m2.invoke(p);
14         System.out.println(returnVal1);//Person [name=null, age=0]
15         //对于运行时类中静态方法的调用
16         Method m3 = clazz.getMethod("info");
17         m3.invoke(Person.class);
18         
19         //getDeclaredMethod(String methodName,Class ... params):获取运行时类中声明了的指定的方法
20         Method m4 = clazz.getDeclaredMethod("display",String.class,Integer.class);
21         m4.setAccessible(true);
22         Object value = m4.invoke(p,"CHN",10);//我的国籍是:CHN
23         System.out.println(value);//10
24     }
 1  class Person extends Creature<String> implements Comparable,MyInterface{
 2     public String name;
 3     private int age;
 4     int id;
 5     //创建类时,尽量保留一个空参的构造器。
 6     public Person() {
 7         super();
 8 //        System.out.println("今天天气很闷热");
 9     }
10     public Person(String name) {
11         super();
12         this.name = name;
13     }
14     private Person(String name, int age) {
15         super();
16         this.name = name;
17         this.age = age;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public int getAge() {
26         return age;
27     }
28     public void setAge(int age) {
29         this.age = age;
30     }
31     
32     public int getId() {
33         return id;
34     }
35     public void setId(int id) {
36         this.id = id;
37     }
38     @MyAnnotation(value = "abc123")
39     public void show(){
40         System.out.println("我是一个人!");
41     }
42     
43     private Integer display(String nation,Integer i) throws Exception{
44         System.out.println("我的国籍是:" + nation);
45         return i;
46     }
47     @Override
48     public String toString() {
49         return "Person [name=" + name + ", age=" + age + "]";
50     }
51     @Override
52     public int compareTo(Object o) {
53         // TODO Auto-generated method stub
54         return 0;
55     }
56     
57     public static void info(){
58         System.out.println("中国人!");
59     }
60     
61     class Bird{
62         
63     }
64     
65 }
 1 import static java.lang.annotation.ElementType.CONSTRUCTOR;
 2 import static java.lang.annotation.ElementType.FIELD;
 3 import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
 4 import static java.lang.annotation.ElementType.METHOD;
 5 import static java.lang.annotation.ElementType.PARAMETER;
 6 import static java.lang.annotation.ElementType.TYPE;
 7 
 8 import java.lang.annotation.Retention;
 9 import java.lang.annotation.RetentionPolicy;
10 import java.lang.annotation.Target;
11 
12 @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
13 @Retention(RetentionPolicy.RUNTIME)
14 public @interface MyAnnotation {
15     String value();
16 }
1 public class Creature<T>{
2     public double weight;
3     
4     public void breath(){
5         System.out.println("呼吸!");
6     }
7 }

 1 import java.io.Serializable; 2 3 public interface MyInterface extends Serializable{ 4 5 } 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。