JAVA学習--反射方法操作

24785 ワード

 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 }