ImportJava.lang.reflect.Constructor; ImportJava.lang.reflect.Field; ImportJava.lang.reflect.Method; ImportAndroid.os.Bundle; Importandroid.app.Activity; /*** Demo Description: * Example of use of Java reflection technology in Android * The class that describes the bytecode file (Xxx.class) in Java is called the process of the classes ' reflection, which can be considered as the process of dissecting class*/ Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Init (); } Private voidinit () {Try { /*** 1 reflect the method of construction without parameters and get the object * Note: * 1 should pass in Class.forName () the full name of the class containing the package name * 2 newinstance () The nature of the method is to invoke the non-public construction method of the class*/String className1= "Cn.testreflect.Worker"; Class CLAZZ1=Class.forName (CLASSNAME1); Object Object1=clazz1.newinstance (); System.out.println ("object1.tostring () =" +object1.tostring ()); /*** 2 reflect the construction method with parameters and get the object*/String className2= "Cn.testreflect.Worker"; Class clazz2=Class.forName (className2); Constructor Constructor1=clazz2.getconstructor (int.class, String.class); Object Object2=constructor1.newinstance (18, "Xiaoming"); System.out.println ("object2.tostring () =" +object2.tostring ()); /*** 3 Gets the private field of the class * Note: * Get common fields should call the Clazz3.getfield (name) method*/String ClassName3= "Cn.testreflect.Worker"; Class clazz3=Class.forName (ClassName3); Field AgeField1=clazz3.getdeclaredfield ("Age"); System.out.println ("Agefield1=" +ageField1); /*** 4 Get and change private fields of an object * that is, simulate get () and set () methods*/String className4= "Cn.testreflect.Worker"; Class clazz4=Class.forName (CLASSNAME4); Field ageField2=clazz4.getdeclaredfield ("Age"); Object Object4=constructor1.newinstance (18, "Xiaoming"); //to cancel access to a private field for the legality checkAgefield2.setaccessible (true); //Gets the private field of the objectObject ageobject4=Agefield2.get (OBJECT4); System.out.println ("Ageobject4=" +AgeObject4); //change the value of the private field of the object againAgefield2.set (Object4, 9527); //re-acquiredObject ageobject5=Agefield2.get (OBJECT4); System.out.println ("Ageobject5=" +ageObject5); /*** 5 Method of calling the object with parameters*/String className5= "Cn.testreflect.Worker"; Class Clazz5=Class.forName (CLASSNAME5); Method Method=clazz5.getmethod ("Printmessage", String.class,int.class,int.class); Object Object5=clazz5.newinstance (); Method.invoke (OBJECT5,"Zhou Xing", 50,9527); } Catch(Exception e) {System.out.println (e.tostring ()); } }
Public classWorker {Private intAge ; PrivateString name; PublicWorker () {Super(); System.out.println ("---> Public Worker () {}"); } PublicWorker (intAge , String name) { Super(); This. Age =Age ; This. Name =name; System.out.println ("---> Public Worker (int age, String name) {}"); } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"Worker [age=" + Age + ", name=" + name + "]"; } Public voidPrintmessage (String name,intAgeintsalary) {System.out.println ("Name=" +name+ ", age=" +age+ ", salary=" +salary); } }
Main.xml
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Use of Java reflection technology in Android"android:layout_centerinparent= "true"/> </Relativelayout>
Example of using Java reflection technology in Android