`
lggege
  • 浏览: 373204 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

举例解析Java的反射机制

    博客分类:
  • Java
阅读更多
  • 1. 使用反射得到对象的属性. 注: 属性的使用,同样也受private,public等作用域的限制.

public class FieldClass {
	public String publicField = "ss"; // public 属性
	private Double privateField = new Double(22.22); // private 属性
	public static Boolean staticField = true; // static 属性
}


import java.lang.reflect.Field;
import junit.framework.TestCase;

public class FieldTestCase extends TestCase {
	public void testField() throws Exception {
		FieldClass test = new FieldClass();

		Field field1 = FieldClass.class.getField("publicField");
		assertEquals(field1.get(test), test.publicField); // 能够使用test.intPublicField,则也可以使用反射得到值

		try {
			Field field2 = FieldClass.class.getField("privateField");
		} catch (Exception e) {
			assertTrue(e instanceof java.lang.NoSuchFieldException); // 由于doubleField是private的, 不可被外部直接调用
		}

		Field field3 = FieldClass.class.getField("staticField");
		assertTrue((Boolean) field3.get(FieldClass.class));
	}
}


  • 2. 使用反射得到对象的无参方法.

public class MethodClass {
	public String publicMehod() {
		return "public";
	}
	
	private String privateMethod() {
		return "private";
	}
	
	public static String staticMethod() {
		return "static";
	}
}

import java.lang.reflect.Method;
import junit.framework.TestCase;

public class MethodTestCase extends TestCase {
	public void testMethod() throws Exception {
		MethodClass test = new MethodClass();

		Method method1 = MethodClass.class.getMethod("publicMehod", new Class[] {});
		assertEquals(method1.invoke(test, new Object[] {}), "public");

		try {
			Method method2 = MethodClass.class.getMethod("privateMethod", new Class[] {});
		} catch (Exception e) {
			assertTrue(e instanceof NoSuchMethodException);
		}

		Method method3 = MethodClass.class.getMethod("staticMethod", new Class[] {});
		assertEquals(method3.invoke(MethodClass.class, new Object[] {}), "static");
	}
}



  • 3. 使用反射得到对象的有参方法.

public class ArguementMethodClass {
	public String methodWithNoArguement() {
		return "no";
	}
	
	public String methodWithOneArgument(String arg) {
		return "one";
	}
	
	public String methodWithTwoArguments(String arg0, String arg1) {
		return "two";
	}
	
	public String methodWithArrayArguments(String[] argArray) {
		return "array";
	}
	
	public String methodWithMoreArguments(String... args) {
		return "more";
	}
}

import java.lang.reflect.Method;
import junit.framework.TestCase;

public class ArgumentMethodTestCase extends TestCase {
	public void testArgumentMethod() throws Exception {
		ArguementMethodClass test = new ArguementMethodClass();

		Method method1 = ArguementMethodClass.class.getMethod("methodWithNoArguement", new Class[] {});
		method1.invoke(test, new Object[] {});

		Method method2 = ArguementMethodClass.class.getMethod("methodWithOneArgument", new Class[] { String.class });
		assertEquals(method2.invoke(test, new Object[] { "aa" }), "one");

		Method method3 = ArguementMethodClass.class.getMethod("methodWithTwoArguments", new Class[] { String.class, String.class });
		assertEquals(method3.invoke(test, new Object[] { "aa", "bb" }), "two");

		Method method4 = ArguementMethodClass.class.getMethod("methodWithArrayArguments", new Class[] { String[].class });
		assertEquals(method4.invoke(test, new Object[] { new String[] { "aa" } }), "array");

		Method method5 = ArguementMethodClass.class.getMethod("methodWithMoreArguments", new Class[] { String[].class });
		assertEquals(method5.invoke(test, new Object[] { new String[] { "aa" } }), "more");
	}
}


  • 4. 重载的处理.

public class OverloadMethodClass {
	public String sameMethod() {
		return "one";
	}
	
	public String sameMethod(String arg) {
		return "two";
	}
}
import java.lang.reflect.Method;
import junit.framework.TestCase;

public class OverloadMthodTestCase extends TestCase {
	public void testOverrideMethod() throws Exception {
		OverloadMethodClass test = new OverloadMethodClass();

		Method method1 = OverloadMethodClass.class.getMethod("sameMethod", new Class[] {});
		assertEquals(method1.invoke(test, new Object[] {}), "one");

		Method method2 = OverloadMethodClass.class.getMethod("sameMethod", new Class[] { String.class });
		assertEquals(method2.invoke(test, new Object[] { "aa" }), "two");
	}
}
  • reflect.rar (2.5 KB)
  • 描述: 上面涉及到的代码,可以下载.
  • 下载次数: 44
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics