类反射的简单的例子

 

Code:
  1. import java.lang.reflect.Field;  
  2. import java.lang.reflect.Modifier;  
  3. import java.sql.Timestamp;  
  4. import java.util.Calendar;  
  5.   
  6. public class Exec {  
  7.     public static void main(String[] args) throws Exception {  
  8.         Calendar birthday = Calendar.getInstance();  
  9.         birthday.set(1985721000);  
  10.         Student s1 = new Student("00008""胡歌"truenew Timestamp(  
  11.                 birthday.getTimeInMillis()), 1.80);  
  12.   
  13.         ClassInfo classInfo = new ClassInfo();  
  14.         System.out.println(classInfo.getClassInfo(s1));  
  15.     }  
  16.   
  17. }  
  18.   
  19. // 该类可以获取任何类的元数据信息  
  20. class ClassInfo {  
  21.     public String getClassInfo(Object obj) {  
  22.         StringBuffer result = new StringBuffer();  
  23.         // 得到参数类变量的Class引用变量  
  24.         Class cls = obj.getClass();  
  25.   
  26.         // 得到参数类变量的属性信息  
  27.         Field[] fields = cls.getDeclaredFields();  
  28.   
  29.         // 得到参数类变量的完整类名(含有包的名称)  
  30.         String fullName = cls.getName();  
  31.   
  32.         // 得到去除包名称的类名  
  33.         String className = fullName.substring(fullName.lastIndexOf('.') + 1);  
  34.   
  35.         // 如果有包的定义,可以得到包的名称  
  36.         int packagePosition = fullName.lastIndexOf('.');  
  37.         String packageName = null;  
  38.         if (packagePosition < 0)  
  39.             packageName = "";  
  40.         else  
  41.             packageName = fullName.substring(0, fullName.lastIndexOf('.'));  
  42.   
  43.         // 输出包名和类名  
  44.         result.append("包的名称为:" + packageName + "/n");  
  45.         result.append("类的名称为:" + className + "/n");  
  46.   
  47.         // 输出类中所有的属性信息  
  48.         for (Field field : fields) {  
  49.             // 允许访问私有成员  
  50.             field.setAccessible(true);  
  51.   
  52.             try {  
  53.                 // 输出私有属性信息  
  54.                 if (field.getModifiers() == Modifier.PRIVATE)  
  55.                     result.append("私有属性" + field.getName() + ":值为"  
  56.                             + field.get(obj) + "/n");  
  57.   
  58.                 // 输出受保护属性信息  
  59.                 if (field.getModifiers() == Modifier.PROTECTED)  
  60.                     result.append("受保护属性" + field.getName() + ":值为"  
  61.                             + field.get(obj) + "/n");  
  62.             } catch (Exception e) {  
  63.                 System.out.println(e.getMessage());  
  64.             }  
  65.         }  
  66.         return result.toString();  
  67.     }  
  68. }  
  69.   
  70. // 学生类  
  71. class Student {  
  72.     // 学号  
  73.     private String number = null;  
  74.   
  75.     // 姓名  
  76.     private String name = null;  
  77.   
  78.     // 性别  
  79.     private boolean sex = false;  
  80.   
  81.     // 生日  
  82.     private Timestamp birthday = null;  
  83.   
  84.     // 身高  
  85.     private double height = 0;  
  86.   
  87.     // 默认构造函数  
  88.     public Student() {  
  89.     }  
  90.   
  91.     // 该构造函数可以对学生的属性进行初始化  
  92.     public Student(String number, String name, boolean sex, Timestamp birthday,  
  93.             double height) {  
  94.         setNumber(number);  
  95.         setName(name);  
  96.         setSex(sex);  
  97.         setBirthday(birthday);  
  98.         setHeight(height);  
  99.     }  
  100.   
  101.     // 学号的读取函数  
  102.     public String getNumber() {  
  103.         return number;  
  104.     }  
  105.   
  106.     // 学号的设置函数  
  107.     public void setNumber(String number) {  
  108.         this.number = number;  
  109.     }  
  110.   
  111.     // 姓名的读取函数  
  112.     public String getName() {  
  113.         return name;  
  114.     }  
  115.   
  116.     // 姓名的设置函数  
  117.     public void setName(String name) {  
  118.         this.name = name;  
  119.     }  
  120.   
  121.     // 性别的读取函数  
  122.     public boolean isSex() {  
  123.         return sex;  
  124.     }  
  125.   
  126.     // 性别的设置函数  
  127.     public void setSex(boolean sex) {  
  128.         this.sex = sex;  
  129.     }  
  130.   
  131.     // 生日的读取函数  
  132.     public Timestamp getBirthday() {  
  133.         return birthday;  
  134.     }  
  135.   
  136.     // 生日的设置函数  
  137.     public void setBirthday(Timestamp birthday) {  
  138.         this.birthday = birthday;  
  139.     }  
  140.   
  141.     // 身高的读取函数  
  142.     public double getHeight() {  
  143.         return height;  
  144.     }  
  145.   
  146.     // 身高的设置函数  
  147.     public void setHeight(double height) {  
  148.         this.height = height;  
  149.     }  
  150.   
  151.     // 格式化字符信息输出  
  152.     public String toString() {  
  153.         return "学号:" + number + "/n姓名:" + name + "/n是否为男生:" + sex + "/n生日:"  
  154.                 + birthday + "/n身高:" + height;  
  155.     }  
  156. }  

程序运行结果:

包的名称为:

类的名称为:Student

私有属性number:值为00008

私有属性name:值为胡歌

私有属性sex:值为true

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

私有属性height:值为1.8

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值