import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class RefMethod {
private String uid;
private String uname;
public RefMethod() {
super();
}
public RefMethod(String uid, String uname) {
super();
this.uid = uid;
this.uname = uname;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public static Object reflect(Object obj, String methodName, Class[] argumentsType, Object[] argumentsValue){
try {
Class myClass = obj.getClass();
Method method = myClass.getMethod(methodName, argumentsType);
Object resultValue = method.invoke(obj, argumentsValue);
return resultValue;
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public void testOne(){
System.out.println("testOne方法:无参数无返回值");
}
public Double testFour(){
System.out.print("testFour方法:无参数有返回值:");
return 33.00;
}
public void testTwo(Integer x){ //这是包装类型
System.out.println("testTwo方法:有参数无返回值:"+"\t"+x);
}
public String testThree(String x){ //这是包装类型
return "testThree方法:有参数有返回值:"+"\t"+x;
}
public String testFive(String x,Integer y, Date d, Double e, Float f, Timestamp ts){ //这是包装类型
return "testFive方法:多个参数有返回值:"+"\t"+x+"\t"+y+"\t"+d+"\t"+e+"\t"+f+"\t"+ts;
}
public void testSix(int x){
System.out.println("testSix方法:一个基本数据类型参数无返回值:"+"\t"+x);
}
public RefMethod testSeven(RefMethod obj){
System.out.print("testSeven方法:一个自定义数据类型参数有返回值:"+"\t"+obj.getUid()+"\t"+obj.getUname()+"\t");
return obj;
}
public void testEight(List<RefMethod> listObj){
for (RefMethod obj : listObj) {
System.out.println("testEight方法:一个List 范型参数无返回值:"+"\t"+obj.getUid()+"\t"+obj.getUname()+"\t");
}
}
public void testNine(List listObj){
for (Object obj : listObj) {
RefMethod rf = (RefMethod) obj;
System.out.println("testNine方法:一个List数据类型参数无返回值:"+"\t"+rf.getUid()+"\t"+rf.getUname()+"\t");
}
}
public void testTen(Map map){
Set keys = map.keySet();
for (Object k : keys) {
String myKey = (String) k;
System.out.println("testTen方法:一个Map数据类型参数无返回值:"+"\t"+map.get(myKey));
}
}
public static void main(String[] args) {
Object resultValue = null;
List li = new ArrayList();
li.add(new RefMethod("001","JAVA"));
li.add(new RefMethod("002","J2EE"));
li.add(new RefMethod("003","JSP"));
Map rmMap = new HashMap();
rmMap.put("上海", "属于中国");
rmMap.put("号百", "属于电信");
rmMap.put("彩铃", "属于中国电信");
// RefMethod.reflect(new RefMethod(),"testOne",new Class[]{},new Object[]{});
// RefMethod.reflect(new RefMethod(),"testTwo",new Class[]{new Integer(0).getClass()},new Object[]{new Integer(56)});
// resultValue = RefMethod.reflect(new RefMethod(),"testThree",new Class[]{new String().getClass()},new Object[]{new String("计算机软件")});
// resultValue = RefMethod.reflect(new RefMethod(),"testFour",new Class[]{},new Object[]{});
/* resultValue = RefMethod.reflect(new RefMethod(),
"testFive",
new Class[]{
new String().getClass(),
new Integer(0).getClass(),
new Date().getClass(),
new Double(0).getClass(),
new Float(0).getClass(),
new Timestamp(0).getClass()},
new Object[]{
new String("String类型"),
new Integer(100000),
new Date(),
new Double(20.00),
new Float(50.0),
new Timestamp(System.currentTimeMillis())});*/
// resultValue = RefMethod.reflect(new RefMethod(),"testSix",new Class[]{int.class},new Object[]{44});
/*RefMethod obj = new RefMethod();
obj.setUid("10010");
obj.setUname("上海电信");
RefMethod resultObj = (RefMethod) RefMethod.reflect(new RefMethod(),"testSeven",new Class[]{RefMethod.class},new Object[]{obj});
System.out.println("返回结果:"+resultObj.getUid()+"\t"+resultObj.getUname());*/
// RefMethod.reflect(new RefMethod(),"testEight",new Class[]{List.class},new Object[]{li});
// RefMethod.reflect(new RefMethod(),"testNine",new Class[]{List.class},new Object[]{li});
RefMethod.reflect(new RefMethod(),"testTen",new Class[]{Map.class},new Object[]{rmMap});
System.out.println("返回结果:"+resultValue);
}
}
分享到:
相关推荐
主要介绍了详解Golang利用反射reflect动态调用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
9.3 Java反射reflect
java反射(reflect)
Java反射 调用空参方法 调用Object类型参数的方法 调用基本类型参数的方法 调用基本类型数组参数的方法 调用String数组参数的方法 调用Object数组参数的方法 调用私有方法
本文章的原代码. 博文链接:https://yidwo.iteye.com/blog/42995
reflect - 在C 17中轻松实现反射和序列化
使用反射(Reflect)获取dll文件中的类型并调用方法,具体内容如下 需引用:System.Reflection; 1. 使用反射(Reflect)获取dll文件中的类型并调用方法(入门案例) static void Main(string[] args) { //dll文件...
JAVA REFLECT (java 反射) 取得类的继承结构 - 类所在的包; |- public Package getPackage() - 类的声明方式; |-取得全类名:public String getName() |-只取类名: public String getSimpleName() - 类所继承...
本例演示了如何使用反射,调用类的静态方法,封装了工具类ClsTool,统一实现静态方法的调用。使用反射,在特定场景下,能够很好的简化代码。例如mybatis
C++ 实现的简单反射示例应用,可以实现类对象的方法的调用
reflect.Value常用方法//数组或切片v.MapIndex(key reflect.Value) //根据key获取对应的值v.Elem() //获取
JAVA反射的基础,基础,但也有价值,可以看看了解, 通过反射可以是程序的扩展性增强!
参数字节码)根据方法名和参数类型获取指定的的方法 methodName:方法名 Class:形参类型。如果方法没有形参,则Class可变参数不用写 Method method = clazz.getDeclaredMethod(String ...