日常学习

Java反射获取基本类型、包装类型、String对象类型的字段值

2017-01-12  本文已影响118人  木叶之荣
package com.zkn.reflect.classinfo;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

import com.zkn.reflect.scope.Company;
import com.zkn.reflect.scope.People;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * 
 * @since:2015-5-25 下午07:02:09
 * 
 */
public class ClassInfo {

 public static void getParameters(Class<?> clazz) {

  Field[] fields = clazz.getDeclaredFields();
  for(Field f : fields){
   System.out.println(f.getName());
  }
 }
 
 public static void getFieldsValue(Object obj) {
  Field[] fields = obj.getClass().getDeclaredFields();
  /**
   * 基本类型、包装类型、String类型
   */
  String[] types = {"java.lang.Integer",
        "java.lang.Double",
        "java.lang.Float",
        "java.lang.Long",
        "java.lang.Short",
        "java.lang.Byte",
        "java.lang.Boolean",
        "java.lang.Character",
        "java.lang.String",
        "int","double","long","short","byte","boolean","char","float"};
  for(Field f : fields){
   f.setAccessible(true);
   try {
    for(String str : types) {
     if(f.getType().getName().equals(str))
      System.out.println("字段:"+f.getName()+" 类型为:"+f.getType().getName()+" 值为:"+ f.get(obj));
    }
   } catch (IllegalArgumentException e) {
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void main(String[] args) throws Exception {
  People people = new People();
  people.setAge(12);
  people.setSex("男");
  Company company = new Company();
  company.setAddress("北京");
  company.setName("科技股份有限公司");
  people.setCompany(company);
  people.setMoney(1000.00);
  getFieldsValue(people);
 }
 
}
package com.zkn.reflect.scope;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * 
 * @since:2015-5-25 下午07:04:22
 * 
 */
public class People {

 /**
  * 姓名
  */
 private String name;
 /**
  * 性别
  */
 private String sex;
 /**
  * 年龄
  */
 private int age;
 /**
  * 地址
  */
 private String address;
 /**
  * 学校
  */
 public String school;
 /**
  * 公司
  */
 private Company company;
 /**
  * 金额
  */
 private Double money;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getSchool() {
  return school;
 }
 public void setSchool(String school) {
  this.school = school;
 }
 public Company getCompany() {
  return company;
 }
 public void setCompany(Company company) {
  this.company = company;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Double getMoney() {
  return money;
 }
 public void setMoney(Double money) {
  this.money = money;
 }
 
}
package com.zkn.reflect.scope;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * 
 * @since:2015-5-28 下午12:52:55
 * 
 */
public class Company {

 /**
  * 公司名称
  */
 private String name;
 /**
  * 公司地址
  */
 private String address;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 
 @Override
 public String toString() {
  return "Company [name=" + name + ", address=" + address + "]";
 }
 
}

输出结果:

字段:name 类型为:java.lang.String 值为:null
字段:sex 类型为:java.lang.String 值为:男
字段:age 类型为:int 值为:12
字段:address 类型为:java.lang.String 值为:null
字段:school 类型为:java.lang.String 值为:null
字段:money 类型为:java.lang.Double 值为:1000.0

上一篇下一篇

猜你喜欢

热点阅读