类反射的简单例子

import java.lang.reflect.Field;  
import java.lang.reflect.Modifier;  
import java.sql.Timestamp;  
import java.util.Calendar;  
  
public class Exec {  
    public static void main(String[] args) throws Exception {  
        Calendar birthday = Calendar.getInstance();  
        birthday.set(1985, 7, 21, 0, 0, 0);  
        Student s1 = new Student("00008", "胡歌", true, new Timestamp(  
                birthday.getTimeInMillis()), 1.80);  
  
        ClassInfo classInfo = new ClassInfo();  
        System.out.println(classInfo.getClassInfo(s1));  
    }  
  
}  
  
// 该类可以获取任何类的元数据信息  
class ClassInfo {  
    public String getClassInfo(Object obj) {  
        StringBuffer result = new StringBuffer();  
        // 得到参数类变量的Class引用变量  
        Class cls = obj.getClass();  
  
        // 得到参数类变量的属性信息  
        Field[] fields = cls.getDeclaredFields();  
  
        // 得到参数类变量的完整类名(含有包的名称)  
        String fullName = cls.getName();  
  
        // 得到去除包名称的类名  
        String className = fullName.substring(fullName.lastIndexOf('.') + 1);  
  
        // 如果有包的定义,可以得到包的名称  
        int packagePosition = fullName.lastIndexOf('.');  
        String packageName = null;  
        if (packagePosition < 0)  
            packageName = "";  
        else  
            packageName = fullName.substring(0, fullName.lastIndexOf('.'));  
  
        // 输出包名和类名  
        result.append("包的名称为:" + packageName + "/n");  
        result.append("类的名称为:" + className + "/n");  
  
        // 输出类中所有的属性信息  
        for (Field field : fields) {  
            // 允许访问私有成员  
            field.setAccessible(true);  
  
            try {  
                // 输出私有属性信息  
                if (field.getModifiers() == Modifier.PRIVATE)  
                    result.append("私有属性" + field.getName() + ":值为"  
                            + field.get(obj) + "/n");  
  
                // 输出受保护属性信息  
                if (field.getModifiers() == Modifier.PROTECTED)  
                    result.append("受保护属性" + field.getName() + ":值为"  
                            + field.get(obj) + "/n");  
            } catch (Exception e) {  
                System.out.println(e.getMessage());  
            }  
        }  
        return result.toString();  
    }  
}  
  
// 学生类  
class Student {  
    // 学号  
    private String number = null;  
  
    // 姓名  
    private String name = null;  
  
    // 性别  
    private boolean sex = false;  
  
    // 生日  
    private Timestamp birthday = null;  
  
    // 身高  
    private double height = 0;  
  
    // 默认构造函数  
    public Student() {  
    }  
  
    // 该构造函数可以对学生的属性进行初始化  
    public Student(String number, String name, boolean sex, Timestamp birthday,  
            double height) {  
        setNumber(number);  
        setName(name);  
        setSex(sex);  
        setBirthday(birthday);  
        setHeight(height);  
    }  
  
    // 学号的读取函数  
    public String getNumber() {  
        return number;  
    }  
  
    // 学号的设置函数  
    public void setNumber(String number) {  
        this.number = number;  
    }  
  
    // 姓名的读取函数  
    public String getName() {  
        return name;  
    }  
  
    // 姓名的设置函数  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    // 性别的读取函数  
    public boolean isSex() {  
        return sex;  
    }  
  
    // 性别的设置函数  
    public void setSex(boolean sex) {  
        this.sex = sex;  
    }  
  
    // 生日的读取函数  
    public Timestamp getBirthday() {  
        return birthday;  
    }  
  
    // 生日的设置函数  
    public void setBirthday(Timestamp birthday) {  
        this.birthday = birthday;  
    }  
  
    // 身高的读取函数  
    public double getHeight() {  
        return height;  
    }  
  
    // 身高的设置函数  
    public void setHeight(double height) {  
        this.height = height;  
    }  
  
    // 格式化字符信息输出  
    public String toString() {  
        return "学号:" + number + "/n姓名:" + name + "/n是否为男生:" + sex + "/n生日:"  
                + birthday + "/n身高:" + height;  
    }  
}  
程序运行结果:

包的名称为:
类的名称为:Student

私有属性number:值为00008

私有属性name:值为胡歌

私有属性sex:值为true

私有属性birthday:值为1985-08-21 00:00:00.264

私有属性height:值为1.8

==============================================================================

本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员

==============================================================================
原文地址:https://www.cnblogs.com/rollenholt/p/2070585.html