An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.关于覆盖,就是要求方法签名(名称与参数)与返回类型保持一致。
你觉得没用是因为你就是为了用而用,那么当然没意义。我可以知道任意方法的调用对象主体信息,方法信息,参数信息,我就可以根据各种情况作出相应处理@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().matches("^(remove|persist|merge)\\w*$")){ System.out.println("检测到事务方法,开启事务"); } if (proxy instanceof String){ System.out.println("这是个SQL字符串而不是个结果集 动态执行它"); } Object o = method.invoke(Class.forName(getName()).newInstance(), args);//这里做你原来要做的事
System.out.println("得到原来的返回值:"+o);
if (method.getReturnType() == ResultSet.class ){ System.out.println("检测到查询方法,将结果装箱"); }
return "改变返回的值"; }
if (proxy instanceof String){ System.out.println("这是个SQL字符串而不是个结果集 动态执行它"); }
方法调用的反射执行模式Object o = method.invoke(obj, args);method是方法对象
obj是该方法对象所在的类对象实例
args是方法参数
返回是Object类型,因为编译时无法获得方法轻易获得所要调用方法的返回类型,因此使用了“万能型”
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (method.getName().matches("^(remove|persist|merge)\\w*$")){
System.out.println("检测到事务方法,开启事务");
} if (proxy instanceof String){
System.out.println("这是个SQL字符串而不是个结果集 动态执行它");
} Object o = method.invoke(Class.forName(getName()).newInstance(), args);//这里做你原来要做的事
System.out.println("得到原来的返回值:"+o);
if (method.getReturnType() == ResultSet.class ){
System.out.println("检测到查询方法,将结果装箱");
}
return "改变返回的值";
}
System.out.println("这是个SQL字符串而不是个结果集 动态执行它");
}
这里看不懂额