java reflect 小例子

public static void main(String[] args) {
        Ticket ticket = new Ticket();
        ticket.setAddress("address00");
        ticket.setCity("city00");
        ticket.setPhone("phone00");
        ticket.setPoil("poi00");
        ticket.setTitle("title00");
        Field[] fields = ticket.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);//这行是必须的
            try {
                System.out.println(field.get(ticket).toString());
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

posted on 2012-09-09 01:54  雨渐渐  阅读(169)  评论(0编辑  收藏  举报

导航