JAVA学习--反射属性操作

 1     //获取对应的运行时类的属性
 2     @Test
 3     public void test1(){
 4         Class clazz = Person.class;
 5         //1.getFields():只能获取到运行时类中及其父类中声明为public的属性
 6         Field[] fields = clazz.getFields();
 7         
 8         for(int i = 0;i < fields.length;i++){
 9             System.out.println(fields[i]);
10         }
11         System.out.println();
12         //2.getDeclaredFields():获取运行时类本身声明的所有的属性
13         Field[] fields1 = clazz.getDeclaredFields();
14         for(Field f : fields1){
15             System.out.println(f.getName());
16         }
17     }
 1 //权限修饰符  变量类型 变量名
 2     //获取属性的各个部分的内容
 3     @Test
 4     public void test2(){
 5         Class clazz = Person.class;
 6         Field[] fields1 = clazz.getDeclaredFields();
 7         for(Field f : fields1){
 8             //1.获取每个属性的权限修饰符
 9             int i = f.getModifiers();
10             String str1 = Modifier.toString(i);
11             System.out.print(str1 + " ");
12             //2.获取属性的类型
13             Class type = f.getType();
14             System.out.print(type.getName() + " ");
15             //3.获取属性名
16             System.out.print(f.getName());
17             
18             System.out.println();
19         }
20     }
 1 //调用运行时类中指定的属性
 2     @Test
 3     public void test3() throws Exception{
 4         Class clazz = Person.class;
 5         //1.获取指定的属性
 6         //getField(String fieldName):获取运行时类中声明为public的指定属性名为fieldName的属性
 7         Field name = clazz.getField("name");
 8         //2.创建运行时类的对象 
 9         Person p = (Person)clazz.newInstance();
10         System.out.println(p);
11         //3.将运行时类的指定的属性赋值
12         name.set(p,"Jerry");
13         System.out.println(p);
14         System.out.println("%"+name.get(p));
15         
16         System.out.println();
17         //getDeclaredField(String fieldName):获取运行时类中指定的名为fieldName的属性
18         Field age = clazz.getDeclaredField("age");
19         //由于属性权限修饰符的限制,为了保证可以给属性赋值,需要在操作前使得此属性可被操作。
20         age.setAccessible(true);
21         age.set(p,10);
22         System.out.println(p);
23         
24 //        Field id = clazz.getField("id");
25         
26     }
 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 }
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value();
}
public class Creature<T>{
    public double weight;
    
    public void breath(){
        System.out.println("呼吸!");
    }
}
import java.io.Serializable;

public interface MyInterface extends Serializable{

}
原文地址:https://www.cnblogs.com/zhangfan94/p/4273221.html