您的位置:首页 > 其它

反射的基本使用实例

2014-12-22 17:22 204 查看
package com.franky.reflex;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @原理:反射就是通过取得指定类名的Class类,来获取类中的成员方法和成员变量
 * @实例:反射的基本使用实例
 * @author Administrator
 *
 */
public class ReflexDemo {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		getObj();
		getMethod1();
		getMethod2();
		getMethod3();
		
	}

	/**
	 * 获取非静态有参方法
	 * @throws Exception
	 */
	private static void getMethod3() throws Exception {
		String name = "com.franky.domain.Student";
		Class clazz = Class.forName(name);
		Method method = clazz.getMethod("show", String.class,int.class);
		Object obj = clazz.newInstance();
		method.invoke(obj , "jim",33);
	}

	/**
	 * 获取静态无参方法
	 * @throws Exception
	 */
	private static void getMethod2() throws Exception {
		String name = "com.franky.domain.Student";
		Class clazz = Class.forName(name);
		Method method = clazz.getMethod("staticShow", null);
		method.invoke(null, null);
	}

	/**
	 * 获取非静态无参方法
	 * @throws Exception
	 */
	private static void getMethod1() throws Exception {
		String name = "com.franky.domain.Student";
		Class clazz = Class.forName(name);
		Method method = clazz.getMethod("show", null);
		Object obj = clazz.newInstance();
		method.invoke(obj , null);
		
	}
	
	/**
	 * 获取对象 和 字段
	 * @throws Exception
	 */
	private static void getObj() throws Exception {
		//定义类名
		String className = "com.franky.domain.Student";
		//得到类的Class对象
		Class clazz = Class.forName(className);
		System.out.println(clazz);
		//实例化无参构造方法
		Object obj = clazz.newInstance();
//		Student s = (Student)obj;
		//实例化有参构造方法
		Constructor cons = clazz.getConstructor(String.class,double.class,double.class,double.class);
		Object  obj1= cons.newInstance("tom",23.5,34.6,23.2);
		//得到私有的字段
		Field filed = clazz.getDeclaredField("sum");
		//取消权限检查
		filed.setAccessible(true);
		//设定字段值
		filed.set(obj1, 55.5);
		//得到字段值
		System.out.println(filed.get(obj1));
	}

}
</pre><p>使用的Student类</p><p></p><pre name="code" class="java">package com.franky.domain;

public class Student implements Comparable<Student>{

	private String name; 
	private double english; 
	private double history; 
	private double math;
	private double sum;
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		long temp;
		temp = Double.doubleToLongBits(sum);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(sum) != Double.doubleToLongBits(other.sum))
			return false;
		return true;
	}

	public Student(String name, double english, double history, double math) {
		super();
		this.name = name;
		this.english = english;
		this.history = history;
		this.math = math;
		this.sum = english+history+math;
	}
	
	public Student(){
		super();
		System.out.println("this is constructer");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getEnglish() {
		return english;
	}

	public void setEnglish(double english) {
		this.english = english;
	}

	public double getHistory() {
		return history;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", sum=" + sum + "]";
	}

	public void setHistory(double history) {
		this.history = history;
	}

	public double getMath() {
		return math;
	}

	public void setMath(double math) {
		this.math = math;
	}

	public void setSum(double sum) {
		this.sum = sum;
	}
	
	public double getSum(){
		return sum;
	}
	@Override
	public int compareTo(Student o) {
		int temp = (int) (this.sum - o.sum);
		return temp==0?this.name.compareTo(o.name):temp;
	}

	public void show(){
		System.out.println("show is running!");
	}
	public static void staticShow(){
		System.out.println("staticshow is running!");
	}
	public void show(String name,int age){
		System.out.println(name+":"+age);
	}
	
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: