Java Reflection Call Demo

Source: Internet
Author: User

Base class. For demo reflex Exercises

1  PackageFanshe;2 3  Public classPerson {4      PublicString name;5     PrivateString age;6     Private intp;7      PublicPerson () {};8      PublicPerson (string name, String age) {9         Super();Ten          This. Name =name; One          This. Age =Age ; A     } -     PrivatePerson (string name, String age,intp) { -         Super(); the          This. Name =name; -          This. Age =Age ; -          This. P =p; -     } +      PublicString GetName () { -         returnname; +     } A @Override at      PublicString toString () { -         return"Person [name=" + name + ", age=" + Age + "]" + "P-" +p; -     } -      Public voidsetName (String name) { -          This. Name =name; -     } in      PublicString getage () { -         returnAge ; to     } +      Public voidsetage (String age) { -          This. Age =Age ; the     } *}

Reflection Demo Class

1  PackageFanshe;2 3 ImportJava.lang.reflect.Constructor;4 ImportJava.lang.reflect.Field;5 Importjava.lang.reflect.InvocationTargetException;6 ImportJava.lang.reflect.Method;7 8  Public classDemo1 {9 Ten      Public Static voidMain (string[] args) { One         //TODO auto-generated Method Stub A         Try { - d_3 (); -}Catch(Exception e) { the             //Todo:handle Exception - e.printstacktrace (); -         } -     } +      -     Private Static voidd_1 () { +Person p =NewPerson (); AClass C =P.getclass (); at      -     } -      -     Private Static voidd_2 () { -Class C = person.class; - System.out.println (c); in     } -     Private Static voidD_3 ()throwsclassnotfoundexception, Nosuchmethodexception, SecurityException, Instantiationexception, Illegalaccessexception, IllegalArgumentException, InvocationTargetException, nosuchfieldexception { toClass C = Class.forName ("Fanshe. Person "); +          -         //--------------------------// theConstructor con = c.getconstructor ();//constructor public to get null arguments *Object o= con.newinstance ();//How to run the obtained constructor $ System.out.println (o.tostring ());Panax Notoginseng          -         //--------------------------// theConstructor Con2 = C.getconstructor (String.class, String.class);//to get a constructor with parameters public +Object O2 = (person) con2.newinstance ("A", "22"); A System.out.println (o2.tostring ()); the          +         //--------------------------// -          $Constructor Con3 = C.getdeclaredconstructor (String.class, String.class,int.class);//get a constructor with parameters private $Con3.setaccessible (true); -Object O3 = (person) con3.newinstance ("A", "22", 14); - System.out.println (o3.tostring ()); the          -         //--------------------------//WuyiObject tmp_obj =c.newinstance (); theField f_name = C.getfield ("name");//GET member variable public -F_name.set (Tmp_obj, "abc");//Setting Variables Wu System.out.println (tmp_obj); -          About         //--------------------------// $Field f_age = C.getdeclaredfield ("Age");//GET member variable public - System.out.println (f_age); -          -         //--------------------------// AMethod m_getname = C.getmethod ("GetName");//Get class function no parameter public +Object O1 = M_getname.invoke (tmp_obj);//run the GetName method. Return value back to obj the System.out.println (O1); -          $         //--------------------------// theMethod m_setname = C.getmethod ("SetName", String.class); theM_setname.invoke (Tmp_obj, "Abcdqweqwe"); the System.out.println (tmp_obj.tostring ()); the     } -}

by reflection. Bypass Generics

1arraylist<string> arr =NewArraylist<string>();2Arr.add ("a");3Class Carr =Arr.getclass ();4Method Arr_add = Carr.getmethod ("Add", Object.class);5Arr_add.invoke (arr, 1);6Arr_add.invoke (arr, 2);7Arr_add.invoke (arr, 3);8Arr_add.invoke (arr, 4);9System.out.println (arr);

Java Reflection Call Demo

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.