举例解析Java的反射机制
程序员文章站
2024-01-21 17:43:40
...
[list]
[*][b]1. 使用反射得到对象的属性. 注: 属性的使用,同样也受private,public等作用域的限制.[/b]
[/list]
[list]
[*][b]2. 使用反射得到对象的无参方法.[/b]
[/list]
[list]
[*][b]3. 使用反射得到对象的有参方法.[/b]
[/list]
[list]
[*][b]4. 重载的处理.[/b]
[/list]
[*][b]1. 使用反射得到对象的属性. 注: 属性的使用,同样也受private,public等作用域的限制.[/b]
[/list]
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));
}
}
[list]
[*][b]2. 使用反射得到对象的无参方法.[/b]
[/list]
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");
}
}
[list]
[*][b]3. 使用反射得到对象的有参方法.[/b]
[/list]
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");
}
}
[list]
[*][b]4. 重载的处理.[/b]
[/list]
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");
}
}
上一篇: java反射机制实例解析
下一篇: 【Java】【设计模式】工厂模式