Methods of reflection classes (where the main method is more special)

Source: Internet
Author: User

 PackageReflet;Importjava.util.List; Public classPerson { PublicString name= "Hahaah";  Public voidAa1 ()//Method{System.out.println ("Aa1"); }     Public voidAa1 (String name,intpassword) {SYSTEM.OUT.PRINTLN (name+":"+password); }     PublicClass[] Aa1 (String name,int[] password) {        return NewClass[]{string.class}; }    Private Static voidAa1 (intnum)    {System.out.println (num); }     Public Static voidMain (string[] args) {System.out.println ("Main!!!"); }}
 PackageReflet;ImportJava.lang.reflect.Method;Importorg.junit.Test; Public classDemo2 {//method of Reflection class: public void Aa1 () {}@Test Public voidTest1 ()throwsException {person P=NewPerson (); Class Clazz=class.forname ("Reflet.person");//Load ClassMethod Method=clazz.getmethod ("Aa1",NULL); Method.invoke (P,NULL);//let the method run, you have to have a P object to run .    }    //method of Reflection class: public void Aa1 (String name,int password)@Test Public voidTest2 ()throwsException {person P=NewPerson (); Class Clazz=class.forname ("Reflet.person");//Load ClassMethod Method=clazz.getmethod ("Aa1", String.class,int.class); Method.invoke (P,"Hemaoyun", 23);//let the method run, you have to have a P object to run .        }        //method of Reflection class: public class[] Aa1 (String name,int[] password)@Test Public voidTest3 ()throwsException {person P=NewPerson (); Class Clazz=class.forname ("Reflet.person");//Load ClassMethod Method=clazz.getmethod ("Aa1", String.class,int[].class); Class ca[]= (class[]) Method.invoke (p, "Hemaoyun",New int[]{1,2,4,5}];//let the method run, you have to have a P object to run .System.out.println (ca[0]); }        //method of Reflection class: private static void Aa1 (int num)@Test Public voidTest4 ()throwsException {person P=NewPerson (); Class Clazz=class.forname ("Reflet.person");//Load ClassMethod Method=clazz.getdeclaredmethod ("Aa1",int.class); Method.setaccessible (true); Method.invoke (P,57);//let the method run, you have to have a P object to run .                                   }                //method of Reflection class: public static void Main (string[] args)@Test Public voidTEST5 ()throwsException {person P=NewPerson (); Class Clazz=class.forname ("Reflet.person");//Load ClassMethod Method=clazz.getdeclaredmethod ("main", string[].class); Method.invoke (NULL, (Object)Newstring[]{"We", "You"}); //Remember, the reflection class calls the Main method, preceded by the object strong, which is the version upgrade compatibility issue. //because the main method is static, you do not need the object P                                                       }}

Methods of reflection classes (where the main method is more special)

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.