利用Class反射获取方法
public class BeanUtil<T> {
void setTime(T t) throws Exception {
Class bean = t.getClass();
Method method = bean.getDeclaredMethod("setCreateTime", Date.class);
method.invoke(t, new Date());
System.out.println(bean.toString());
}
}
public class BeanTest {
public static void main(String[] args) {
User user = new User();
BeanUtil<User> beanUtil = new BeanUtil<User>();
try {
beanUtil.setTime(user);
System.out.println(user.getCreateTime());
} catch (Exception e) {
e.printStackTrace();
}
}
}