java反射基礎應用備忘

來源:互聯網
上載者:User

標籤:異常   getname   his   應用   print   lan   count   create   基礎   

反射機制應用很廣泛。這裡簡單備忘下

package com.shone.ailin;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Reflectdemo {        public static void main(String[] args) throws Exception {                        printVar();                createInstance();        printAllVarName();            }        //列印所有成員變數名    private static void printAllVarName() {        // TODO Auto-generated method stub        Point p = new Point(1,3);        Field[] fields = p.getClass().getDeclaredFields();        for(int i=0;i<fields.length;i++) {            System.out.println("var["+i+"]="+fields[i].getName());        }            }    //列印成員變數的值    private static void printVar() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {        Point p = new Point(1,4);        Field fX = p.getClass().getDeclaredField("x");        fX.setAccessible(true); //這裡必須要加上 。不然會拋出異常。因為是私人成員變數,所以,需要暴力的反射        int x = (int)fX.get(p);        System.out.println("x="+x);            }    //建立執行個體化對象並調用方法    private static void createInstance() throws Exception, SecurityException {        Constructor<Point> con = Point.class.getConstructor(int.class,int.class);        try {            Point instance = con.newInstance(1,3);                        Method m = instance.getClass().getDeclaredMethod("addCount", int.class,int.class);            m.setAccessible(true);            try {                int count = (int) m.invoke(instance,2,7);                System.out.println(count);                            } catch (IllegalAccessException e) {                e.printStackTrace();            } catch (IllegalArgumentException e) {                e.printStackTrace();            } catch (InvocationTargetException e) {                e.printStackTrace();            }                                } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (SecurityException e) {            e.printStackTrace();        }    }    }

Point類

package com.shone.ailin;public class Point {    private int x ;    public int y ;        public Point(int x ,int y ) {        // TODO Auto-generated constructor stub        super();        this.x = x;        this.y = y;    }        private int addCount(int x,int y) {        return x+y;    }}

 

java反射基礎應用備忘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.