获取类的元数据信息


import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Exec {
public static void main(String[] args) {
Calendar birthday = Calendar.getInstance();
birthday.set(1983, 5, 13, 18, 30, 05);
Student s1 = new Student("000005", "张三", true, new Timestamp(birthday
.getTimeInMillis()), 178.3);
System.out.println(s1);

System.out.println("\n==========================================");
System.out.println("通过反射获取的信息:");
System.out.println("==========================================");
ClassInfo classInfo = new ClassInfo();
System.out.println(classInfo.getClassInfo(s1));
}
}

class ClassInfo {
public String getClassInfo(Object obj) {
StringBuffer result = new StringBuffer();
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");
} else if (field.getModifiers() == Modifier.PROTECTED) {
result.append("受保护的属性:" + field.getName() + ":值为:"
+ field.get(obj) + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result.toString();
}
}

class Student {
private String number = null;
private String name = null;
protected boolean sex = false;
private Date birthday = null;
protected double height = 0;

public Student() {
}

public Student(String number, String name, boolean sex, Date birthday,
double height) {
this.number = number;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.height = 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 Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E a HH:mm:ss ");
return "学号:" + number + "\n姓名:" + name + "\n是否为男生:" + sex + "\n生日:"
+ sdf.format(birthday) + "\n身高:" + height;

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值