• Android中Java反射技术的使用示例


    MainActivity如下:

    package cn.testreflect;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import android.os.Bundle;
    import android.app.Activity;
    /**
     * Demo描述:
     * Android中Java反射技术的使用示例
     * 在Java中描述字节码文件(xxx.class)的类叫Class
     * 反射的过程可视为剖析Class的过程
     */
    public class MainActivity extends Activity {
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		init();
    	}
        private void init(){
        	try {
        		/**
        		 * 1 反射出无参的构造方法并得到对象
        		 * 注意:
        		 *      1 在Class.forName()中应该传入含有包名的类全名
        		 *      2 newInstance()方法的本质是调用类的无参Public构造方法
        		 */
        		String className1="cn.testreflect.Worker";
            	Class clazz1=Class.forName(className1);
            	Object object1=clazz1.newInstance();
            	System.out.println("object1.toString()="+object1.toString());
            	
            	/**
            	 * 2 反射出带参数的构造方法并得到对象
            	 */
            	String className2="cn.testreflect.Worker";
            	Class clazz2=Class.forName(className2);
            	Constructor constructor1=clazz2.getConstructor(int.class,String.class);
            	Object object2=constructor1.newInstance(18,"小明");
            	System.out.println("object2.toString()="+object2.toString());
            	
            	/**
            	 * 3 获取类的私有字段
            	 * 注意:
            	 *      获取共有字段应调用clazz3.getField(name)方法
            	 */
            	String className3="cn.testreflect.Worker";
            	Class clazz3=Class.forName(className3);
            	Field ageField1=clazz3.getDeclaredField("age");
            	System.out.println("ageField1="+ageField1);
            	
            	/**
            	 * 4 获取和更改某个对象的私有字段
            	 *   即模拟get()和set()方法
            	 */
            	String className4="cn.testreflect.Worker";
            	Class clazz4=Class.forName(className4);
            	Field ageField2=clazz4.getDeclaredField("age");
            	Object object4=constructor1.newInstance(18,"小明");
            	//取消访问私有字段的合法性检查
            	ageField2.setAccessible(true);
            	//获取对象的私有字段
            	Object ageObject4=ageField2.get(object4);
            	System.out.println("ageObject4="+ageObject4);
            	
            	//再更改对象的私有字段的值
            	ageField2.set(object4, 9527);
            	//重新获得
            	Object ageObject5=ageField2.get(object4);
            	System.out.println("ageObject5="+ageObject5);
            	
            	/**
            	 * 5 调用对象的带参数的方法
            	 */
            	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, "周星星",50,9527);
            	
            	
    		} catch (Exception e) {
    			System.out.println(e.toString());
    		}
        
        }
    
    }
    


    Worker如下:

    package cn.testreflect;
    
    public class Worker {
        private int age;
        private String name;
        public Worker() {
    		super();
    		System.out.println("---> public Worker(){ }");
    	}
    	public Worker(int age, String name) {
    		super();
    		this.age = age;
    		this.name = name;
    		System.out.println("---> public Worker(int age, String name){ }");
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	@Override
    	public String toString() {
    		return "Worker [age=" + age + ", name=" + name + "]";
    	}
    	
    	public void printMessage(String name,int age,int salary){
    		System.out.println("name="+name+",age="+age+",salary="+salary);
    	}
        
    }
    

    main.xml如下:

    <RelativeLayout xmlns: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"
        >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android中Java反射技术的使用" 
            android:layout_centerInParent="true"/>
    
    </RelativeLayout>


     

  • 相关阅读:
    shell进行mysql统计
    java I/O总结
    Hbase源码分析:Hbase UI中Requests Per Second的具体含义
    ASP.NET Session State Overview
    What is an ISAPI Extension?
    innerxml and outerxml
    postman
    FileZilla文件下载的目录
    how to use webpart container in kentico
    Consider using EXISTS instead of IN
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3283644.html
Copyright © 2020-2023  润新知