码迷,mamicode.com
首页 > 编程语言 > 详细

java反射系列五之获取类的完整结构

时间:2019-01-31 00:13:01      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:运行   generated   数字   time   double   类的声明   pack   todo   import   

 

代码示例

Person类

package reflect;

@MyAnnotation(value = "guozi")
public class Person extends Creature<String> implements Comparable,MyInterface{
    public String name;
    private int age;
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @MyAnnotation(value = "123")
    public void show() {
        System.out.println("我是巫妖果子");
    }
    private void display(String nation) throws Exception{
        System.out.println("我的国籍是: "+nation);
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }
    class Bird{
        
    }
}

Person的父类

package reflect;

public class Creature<T> {
    public double weight;
    
    public void breath() {
        System.out.println("呼吸!");
    }
}

Person实现的接口

package reflect;

import java.io.Serializable;

public interface MyInterface extends Serializable{

}

注释

package reflect;

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();
}

有了这些"复杂"条件,我们可以:

获取属性  属性类型  属性修饰权限

package reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class TestFiled {
    public static void main(String[] args) {
        TestFiled t = new TestFiled();
        t.test();
        t.test1();
    }
    public void test() {
        Class clazz = Person.class;
        //1.getFields():只能获取到运行时类中及其父类中声明为public的属性
        Field[] fields = clazz.getFields();
        for(int i= 0; i<fields.length; i++) {
            System.out.println(fields[i]);
        }
        System.out.println();
        //2.getDeclareFields():获取运行时类本身声明的所有属性
        Field [] fields1 = clazz.getDeclaredFields();
        for(Field f:fields1) {
            System.out.println(f.getName());
        }
    }
    //获取对应的运行时类的属性
    public void test1() {
        Class clazz = Person.class;
        //得到运行时类的声明属性
        Field [] fields1 = clazz.getDeclaredFields();
        for(Field f:fields1) {
            //1.获取每个属性的权限修饰符
            //0代表default修饰符  1代表public修饰符   2 代表private修饰符
            int i = f.getModifiers();
            //将代表修饰符的数字转化成字符
            String str1 = Modifier.toString(i);
            System.out.print(str1+" ");
            //2.获取属性的类型
            Class type = f.getType();
            System.out.print(type.getName()+" ");
            //3.获取属性名
            System.out.println(f.getName());
        }
    }
}

技术分享图片

待续...

java反射系列五之获取类的完整结构

标签:运行   generated   数字   time   double   类的声明   pack   todo   import   

原文地址:https://www.cnblogs.com/zjm1999/p/10340191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!