Java Reflection Acquisition Method

Source: Internet
Author: User

 Packagecn.itcast_03;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Method; Public classHour09_reflect { Public Static voidMain (string[] args)throwsexception{//TODO auto-generated Method Stub//Get bytecode File objectClass C = class.forname ("Cn.itcast_01.Person"); //get all the public methods of yourself and Father//method[] methods = C.getmethods (); //get all of your own methods//method[] methods = C.getdeclaredmethods ();//For (Method method:methods) {//System.out.println (method);//         }        /** Person p = new person ();         * P.show (); */        //Creating ObjectsConstructor con =C.getconstructor (); Object obj=con.newinstance (); //get a single methodMethod m1 = C.getmethod ("Show");        M1.invoke (obj); System.out.println ("-------"); Method m2= C.getmethod ("Method", String.class); M2.invoke (obj,"Hello"); Method M3= C.getmethod ("getString", String.class,int.class); Object objstring= M3.invoke (obj, "Hello", 100); System.out.println (objstring);//string s = (string) m3.invoke (obj, "hello", +);//System.out.println (s); //Get PrivateMethod M4 = C.getdeclaredmethod ("function"); M4.setaccessible (true);    M4.invoke (obj); }}

Java Reflection Acquisition Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.