Java--Reflect(反射)專題5——方法反射的基本操作

來源:互聯網
上載者:User

標籤:反射   java   

轉載請註明:http://blog.csdn.net/uniquewonderq

1.如何擷取某個方法

方法的名稱和方法的參數列表才能唯一決定某個方法

2.方法的反射操作

method.invoke(對象,參數列表)

1.擷取一個方法就是擷取類的資訊,擷取類的資訊,首先要擷取類的類類型。

 2.擷取方法名稱和參數列表來決定
            getMethod擷取的是public的方法
            getDeclaredMethod擷取所有聲明的方法

<span style="font-size:18px;">package com.test;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author wonderq */public class MethodReflect {    public static void main(String[] args)  {        //擷取print方法,但是有兩個,這裡擷取print(int ,int)        //1.擷取一個方法就是擷取類的資訊,擷取類的資訊,首先要擷取類的類類型。        A a1=new A();        Class c=a1.getClass();        try {            /*            2.擷取方法名稱和參數列表來決定            getMethod擷取的是public的方法            getDeclaredMethod擷取所有聲明的方法            */            Method m=c.getMethod("print", int.class,int.class);//第二個參數是變參            //方法的反射操作;           // a1.print(10, 20);方法的反射操作是用m對象來進行方法調用和a1.print調用的效果是一樣的            //方法如果沒有傳回值,返回null,有傳回值返回具體的傳回值            //Object o=m.invoke(a1, new Object[]{10,20});            Object o=m.invoke(a1, 10,20);            System.out.println("=========================");            //擷取方法print(string,string)的對象            Method m1=c.getMethod("print", String.class,String.class);            //對方法進行反射操作。            //a1.print("hello","world");            o=m1.invoke(a1, "hello","WORld");//與上述效果一致            System.out.println("=========================");            Method m2=c.getMethod("print");//這個時候第二個參數,沒有,那麼就不傳,變參可以為空白            m2.invoke(a1);        } catch (NoSuchMethodException ex) {            ex.printStackTrace();        } catch (SecurityException ex) {            ex.printStackTrace();        } catch (IllegalAccessException ex) {            Logger.getLogger(MethodReflect.class.getName()).log(Level.SEVERE, null, ex);        } catch (IllegalArgumentException ex) {            Logger.getLogger(MethodReflect.class.getName()).log(Level.SEVERE, null, ex);        } catch (InvocationTargetException ex) {            Logger.getLogger(MethodReflect.class.getName()).log(Level.SEVERE, null, ex);        }    }    }class A{    public void print(){        System.out.println("helloworld!");    }    public void print(int a,int b){        System.out.println("a+b="+(a+b));    }    public void print(String a,String b){        System.out.println(a.toUpperCase()+","+b.toLowerCase());    }}</span>

輸出結果:

run:
a+b=30
=========================
HELLO,world
=========================
helloworld!

這就是擷取方法對象,然後用方法對象進行反射操作。

Java--Reflect(反射)專題5——方法反射的基本操作

聯繫我們

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