User user = new User();
user.setTime5Flag("test");
Class<?> cls = Class.forName("com.test.User");
//接口必须public,无论是否在本类内部使用!或者使用cls.getDeclaredMethod(),或者遍历修改可访问性
Method method = cls.getMethod("getTime5Flag");
String res1 = (String) method.invoke(user);
System.out.println(res1);
//涉及到基本类型如int,则使用int.class!Integer.class!=int.class!
method = cls.getMethod("setTime5Flag", String.class);
method.invoke(user, "Rollen");
method = cls.getMethod("getTime5Flag");
String res2 = (String) method.invoke(user);
System.out.println(res2);
user.getClass().getName();//全路径类名 user.getClass().getSimpleName();//无包名的类名
Class.forName("com.test.User");
com.test.User.class;
user.getClass();
User user = (User) cls.newInstance();//必须有无参构造函数
Constructor<?> cons[]=cls.getConstructors(); //按声明顺序返回 cons[0].newInstance();//无显示声明,则有默认构造函数
Class<?> intes[] = cls.getInterfaces();
cls.getSuperClass();
int mo = cls.getModifiers(); int mo = cons[0].getModifiers(); int mo = method.getModifiers(); Modifier.toString(mo);
method.getParametors(); cons[0].getParametors();
method.getParametorTypes(); cons[0].getParametorTypes();
method.getExceptionTypes();
Field[] field = cls.getDeclaredFields(); //包括private field[0].getModifiers(); field[0].getType();
cls.getFields();
field.setAccessible(true); field.set(obj,'ces'); field.get(obj);
int[] temp={1,2,3,4,5};
Class<?> demo = temp.getClass().getComponentType();
System.out.println("数组类型: "+demo.getName());//int
System.out.println("数组长度: "+Array.getLength(temp));//5
System.out.println("数组的第一个元素: "+Array.get(temp, 0));//1
Array.set(temp, 0, 100);
System.out.println("修改之后数组第一个元素为: "+Array.get(temp, 0));//100
cls.getComponentType();
cls.isArray();
public interface Count {
public void queryCount();
}
public class CountImpl implements Count {
public void queryCount() {
System.out.println("查看账户方法...");
}
}
//代理类
public class CountProxy implements Count {
private CountImpl countImpl;
public CountProxy(CountImpl countImpl) {
this.countImpl = countImpl;
}
@Override
public void queryCount() {
System.out.println("事务处理之前");
countImpl.queryCount(); // 调用委托类的方法;
System.out.println("事务处理之后");
}
}
//测试类
public class TestCount {
public static void main(String[] args) {
CountImpl countImpl = new CountImpl();
CountProxy countProxy = new CountProxy(countImpl);
countProxy.queryCount();
}
}
interface Subject {
public String say(String name, int age);
}
class RealSubject implements Subject {
@Override
public String say(String name, int age) {
return name + " " + age;
}
}
//JDK动态代理类
class MyInvocationHandler implements InvocationHandler {
private Object target = null;
//绑定委托对象并返回一个代理类
public Object bind(Object target) {
this. target = target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(), this); //要绑定接口(cglib弥补了这一点)
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(“before method!”);
Object temp = method.invoke(target, args);
System.out.println(“after method!”);
return temp;
}
}
class hello {
public static void main(String[] args) {
MyInvocationHandler demo = new MyInvocationHandler();
Subject sub = (Subject) demo.bind(new RealSubject());
String info = sub.say("Rollen", 20);
System.out.println(info);
}
}
public interface BookFacade {
public void addBook();
}
public class BookFacadeImpl1 {
public void addBook() {
System.out.println("增加图书的普通方法...");
}
}
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
//cglib动态代理类
public class BookFacadeCglib implements MethodInterceptor {
private Object target;
//绑定委托对象并返回一个代理类
public Object getInstance(Object target) {
this.target = target;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.target.getClass());
// 回调方法
enhancer.setCallback(this);
// 创建代理对象
return enhancer.create();
}
@Override
// 回调方法
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
System.out.println("事物开始");
Object temp = proxy.invokeSuper(obj, args);
System.out.println("事物结束");
return temp;
}
}
public class TestCglib {
public static void main(String[] args) {
BookFacadeCglib cglib = new BookFacadeCglib();
BookFacadeImpl1 bookCglib = (BookFacadeImpl1)cglib.getInstance(new BookFacadeImpl1());
bookCglib.addBook();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有