转:http://blog.csdn.net/lfdfhl/article/details/10311505
1 package com.nthm.androidtestActivity; 2 3 import java.io.File; 4 import java.lang.reflect.Constructor; 5 import java.lang.reflect.Field; 6 import java.lang.reflect.Method; 7 import com.nthm.androidtest.R; 8 import android.os.Bundle; 9 import android.app.Activity; 10 import android.content.ContextWrapper; 11 /** 12 * Demo描述: 13 * Android中Java反射技术的使用示例 14 * 在Java中描述字节码文件(xxx.class)的类叫Class 15 * 反射的过程可视为剖析Class的过程 16 */ 17 public class MainActivity extends Activity { 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 init(); 23 } 24 private void init(){ 25 try { 26 /** 27 * 1 反射出无参的构造方法并得到对象 28 * 注意: 29 * 1 在Class.forName()中应该传入含有包名的类全名 30 * 2 newInstance()方法的本质是调用类的无参Public构造方法 31 */ 32 String className1="com.nthm.androidtestBean.Worker"; 33 Class clazz1=Class.forName(className1); 34 Object object1=clazz1.newInstance(); 35 System.out.println("object1.toString()="+object1.toString()); 36 /** 37 * 2 反射出带参数的构造方法并得到对象 38 */ 39 String className2="com.nthm.androidtestBean.Worker"; 40 Class clazz2=Class.forName(className2); 41 Constructor constructor1=clazz2.getConstructor(int.class,String.class); 42 Object object2=constructor1.newInstance(18,"小明"); 43 System.out.println("object2.toString()="+object2.toString()); 44 45 /** 46 * 3 获取类的私有(private)字段 47 * 注意: 48 * 获取共有(public)字段应调用clazz3.getField(name)方法 49 */ 50 String className3="com.nthm.androidtestBean.Worker"; 51 Class clazz3=Class.forName(className3); 52 Field ageField1=clazz3.getDeclaredField("age"); 53 System.out.println("ageField1="+ageField1); 54 55 /** 56 * 4 获取和更改某个对象的私有字段 57 * 即模拟get()和set()方法 58 */ 59 String className4="com.nthm.androidtestBean.Worker"; 60 Class clazz4=Class.forName(className4); 61 Field ageField2=clazz4.getDeclaredField("age"); 62 Object object4=constructor1.newInstance(18,"小明"); 63 //取消访问私有字段的合法性检查 64 ageField2.setAccessible(true); 65 //获取对象的私有字段 66 Object ageObject4=ageField2.get(object4); 67 System.out.println("ageObject4="+ageObject4.getClass().getName()); 68 System.out.println("ageObject4="+ageObject4); 69 70 //再更改对象的私有字段的值 71 ageField2.set(object4, 9527); 72 //重新获得 73 Object ageObject5=ageField2.get(object4); 74 System.out.println("ageObject5="+ageObject5); 75 76 /** 77 * 5 调用对象的带参数的方法 78 */ 79 String className5="com.nthm.androidtestBean.Worker"; 80 Class clazz5=Class.forName(className5); 81 Method method=clazz5.getMethod("printMessage", String.class,int.class,int.class); 82 Object object5=clazz5.newInstance(); 83 method.invoke(object5, "周星星",50,9527); 84 85 //获取ContextWrapper对象中的mBase对象,该变量保存了ContextImpl对象 86 Field field=ContextWrapper.class.getDeclaredField("mBase"); 87 //跳过属性的修饰(public private protect)检查 88 field.setAccessible(true); 89 //获取对象的指定属性的值 90 Object obj=field.get(this); 91 //根据获取的值所属的类来获取该类中的其他属性 92 field=obj.getClass().getDeclaredField("mPreferencesDir"); 93 //跳过属性的修饰(public private protect)检查 94 field.setAccessible(true); 95 File file=new File("/sacard"); 96 //对该属性重新赋值 97 field.set(obj, file); 98 } catch (Exception e) { 99 System.out.println(e.toString()); 100 } 101 102 } 103 104 }
1 package com.nthm.androidtestBean; 2 3 public class Worker { 4 private int age; 5 private String name; 6 public Worker() { 7 super(); 8 System.out.println("---> public Worker(){ }"); 9 } 10 public Worker(int age, String name) { 11 super(); 12 this.age = age; 13 this.name = name; 14 System.out.println("---> public Worker(int age, String name){ }"); 15 } 16 public int getAge() { 17 return age; 18 } 19 public void setAge(int age) { 20 this.age = age; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 @Override 29 public String toString() { 30 return "Worker [age=" + age + ", name=" + name + "]"; 31 } 32 33 public void printMessage(String name,int age,int salary){ 34 System.out.println("name="+name+",age="+age+",salary="+salary); 35 } 36 37 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:text="Android中Java反射技术的使用" 11 android:layout_centerInParent="true"/> 12 13 </RelativeLayout>
时间: 2024-10-09 00:49:19