java反射取對象中的各表量值

來源:互聯網
上載者:User

package d;

import java.lang.reflect.Field;
 
 

public class bean01 {

    public static void main(String[] args) {
        bean02 bean = new bean02();bean.setId(222);
        System.out.println(getPojoLog(bean));

    }
    
    //列出一個pojo所有的內容,以便測試時檢查資料
    public   static   String   getPojoLog(Object   pojo){
        Class   c   =   pojo.getClass();
        StringBuffer   out   =   new   StringBuffer(c.getName()   + ":   \n ");
        Field[]   fields   =   c.getDeclaredFields();//或者     Field[]   fields   =   c.getFields(); 2者的區別見附錄
        Field.setAccessible(fields, true);
        for   (int   i   =   0;   i   <   fields.length;   i++)   {
            Field   field   =   fields[i];
            String   name   =   field.getName();
            if(name.startsWith( "SF_ ")){
                continue;
            }
            try   {
                Object   vo   =   field.get(pojo);
                String   value   =   " ";
                if   (vo   !=   null)   {
                    value   =   field.get(pojo).toString();
                }
                out.append( "         "+   name   + "   =   "   +   value   +   "\n ");

            }   catch   (Exception   e)   {
                e.printStackTrace();
            }
        }
        return   out.toString();
    }
}

package d;
public class bean02{

    public int id;

    public String[] love;

    public String[] getLove() {
        return love;
    }

    public void setLove(String[] love) {
        this.love = love;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

附錄:

  • 1.package study.reflection;     
  • 2.    
  • 3.public class People {     
  • 4.public String name = null;     
  • 5.private String sex = null;     
  • 6.private String age = null;     
  • 7.private String tel = null;     
  • 8.private String address = null;     
  • 9.public static String s = null;     
  • 10.static {     
  • 11.   System.out.println("loading People");     
  • 12.}     
  • 13.    
  • 14.public static void showPeople() {     
  • 15.    
  • 16.}     
  • 17.    
  • 18.public People(String name) {     
  • 19.   this.name = name;     
  • 20.}     
  • 21.    
  • 22.private People() {     
  • 23.   this.name = name;     
  • 24.}     
  • 25.    
  • 26.private void showPeopleInfo() {     
  • 27.   System.out.println(name + " " + sex + " " + age + " " + tel + " "    
  • 28.     + address);     
  • 29.}     
  • 30.    
  • 31.public String getName() {     
  • 32.   return name;     
  • 33.}     
  • 34.    
  • 35.public void setName(String name) {     
  • 36.   this.name = name;     
  • 37.}     
  • 38.    
  • 39.public String getSex() {     
  • 40.   return sex;     
  • 41.}     
  • 42.    
  • 43.public void setSex(String sex) {     
  • 44.   this.sex = sex;     
  • 45.}     
  • 46.    
  • 47.public String getAge() {     
  • 48.   return age;     
  • 49.}     
  • 50.    
  • 51.public void setAge(String age) {     
  • 52.   this.age = age;     
  • 53.}     
  • 54.    
  • 55.public String getTel() {     
  • 56.   return tel;     
  • 57.}     
  • 58.    
  • 59.public void setTel(String tel) {     
  • 60.   this.tel = tel;     
  • 61.}     
  • 62.    
  • 63.public String getAddress() {     
  • 64.   return address;     
  • 65.}     
  • 66.    
  • 67.public void setAddress(String address) {     
  • 68.   this.address = address;     
  • 69.}     
  • 70.    
  • 71.}     
  • 72.    
  • 73.package esg;     
  • 74.    
  • 75.import java.lang.reflect.Constructor;     
  • 76.import java.lang.reflect.Field;     
  • 77.import java.lang.reflect.Method;     
  • 78.    
  • 79.import study.reflection.People;     
  • 80.    
  • 81.public class Esg {     
  • 82.    
  • 83.public static void main(String[] a) throws ClassNotFoundException {     
  • 84.   Class c1 = People.class;     
  • 85.    
  • 86.   Field[] fs = c1.getFields();     
  • 87.   System.out.println("*******getFields()*********");     
  • 88.   for (int i = 0; i < fs.length; i++) {     
  • 89.    System.out.println(fs[i].getName());     
  • 90.   }     
  • 91.   System.out.println("*******getDeclaredFields()*********");     
  • 92.   fs = c1.getDeclaredFields();     
  • 93.   for (int i = 0; i < fs.length; i++) {     
  • 94.    System.out.println(fs[i].getName());     
  • 95.   }     
  • 96.   System.out.println("*******getMethods()*********");     
  • 97.   Method[] md = c1.getMethods();     
  • 98.   for (int i = 0; i < md.length; i++) {     
  • 99.    System.out.println(md[i].getName());     
  • 100.   }     
  • 101.   System.out.println("*******getDeclaredMethods()*********");     
  • 102.   md = c1.getDeclaredMethods();     
  • 103.   for (int i = 0; i < md.length; i++) {     
  • 104.    System.out.println(md[i].getName());     
  • 105.   }     
  • 106.    
  • 107.   System.out.println("*******getConstructors()*********");     
  • 108.   Constructor[] con = c1.getConstructors();     
  • 109.   for (int i = 0; i < con.length; i++) {     
  • 110.    System.out.println(con[i].getName());     
  • 111.   }     
  • 112.   System.out.println("*******getDeclaredConstructors()*********");     
  • 113.   con = c1.getDeclaredConstructors();     
  • 114.   for (int i = 0; i < con.length; i++) {     
  • 115.    System.out.println(con[i].getName());     
  • 116.   }     
  • 117.}     
  • 118.}    

    * getFields()與getDeclaredFields()區別:getFields()只能訪問類中聲明為公有的欄位,私人的欄位它無法訪問.getDeclaredFields()能訪問類中所有的欄位,與public,private,protect無關
     

    * getMethods()與getDeclaredMethods()區別:getMethods()只能訪問類中聲明為公有的方法,私人的方法它無法訪問,能訪問從其它類繼承來的公有方法.getDeclaredFields()能訪問類中所有的欄位,與public,private,protect無關,不能訪問從其它類繼承來的方法
     

    * getConstructors()與getDeclaredConstructors()區別:getConstructors()只能訪問類中聲明為public的建構函式.getDeclaredConstructors()能訪問類中所有的建構函式,與public,private,protect無關 

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.