java反射和静态代理模式

1 要想使用反射,首先要有一个所需要的类的Class对象,有三种方式获得Class对象:

  1.对象.getClass()方法

2.类名.class

3.Class.forName("java.lang.Object")


2 不带参数的构造方法生成对象的方式:

1.Class<?> classType = String.class;

Object obj = classType.newInstance();

2.Constructor constructor1 = Class对象.getConstructor(new Class[] {});

Object obj = constructor1.neeInstance(new Class[] {});


3 带参数的构造方法生成对象的方式仅有一种:

Constructor constructor1 = Class对象.getConstructor(new Class[] {String.class});

Object obj = constructor1.neeInstance(new Class[] {String.class});


4 Integer.Type返回的是int,Integer.Class返回的是Integer.Class.


5 要想使用方法,在获得Class对象之后,要获得Method对象

Method me = Class对象.getMethod(方法名, 由参数列表的类型的Class对象组成的Class[]数组);

getMethod(String name, Class<?>... parameterTypes)

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

之后 String str = (String)method.invoke(p, new Object[]{"zhangsan"});

invoke()返回Object对象,进行转型

6 method.setAccessible(true);//压制Java的访问控制检查
getDeclaredMethod()获得所有方法   getMethod()获得public方法

7 使用数据 ,Field f = Class对象.getField("数据的name");

getField(String name)
Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

field.set(p, "lisi");

set(Object obj, Object value)
Sets the field represented by this Field object on the specified object argument to the specified new value.

8 数组

1.
public class ArrayTester
{
public static void main(String[] args) throws Exception
{
Class<?> classType = Class.forName("java.lang.String");
Object array = Array.newInstance(classType, 10);
Array.set(array, 5, "hello");
String str = (String)Array.get(array, 5);
System.out.println(str);
}


}

2.
public class ArrayTester2
{
public static void main(String[] args)
{
int[] dims = new int[]{5, 10, 15};
   System.out.println(Integer.TYPE);//返回一个原生数据类型
   System.out.println(Integer.class);//返回一个Integer类对应的Class对象
   
Object array = Array.newInstance(Integer.TYPE, dims);//创建一个长宽高分别为5,10,15的数组
Object arrayObj = Array.get(array, 3);
Class<?> classType = arrayObj.getClass().getComponentType();//组建类型
System.out.println(classType);
   arrayObj = Array.get(arrayObj, 5);
   Array.setInt(arrayObj, 10, 37);
   int[][][] arrayCast = (int[][][]) array;
   System.out.println(arrayCast[3][5][10]);
   //63.33    
}
}
    
9 代理模式

房主 中介 客户

真实角色 代理角色 客户

真实角色和代理角色来自共同一个接口,代理角色封装了真实角色,客户端通过代理角色来调用真实角色的行为

动态代理的优势:1.可以接收任何的真实角色 2.可以接受所接受的Object(一般都为真实角色)的各种方法

return Proxy.newProxyInstance(classType.getClassLoader(), 
classType.getInterfaces(), new VectorProxy(obj));

动态代理是在运行时候动态生成一个实现classType().getInterfaces()接口的类,最后的参数就是传进来的继承自InvocationHander的类

每个真实角色都对应一个继承自InvocationHandler的类,封装真实角色的代理类的方法如果要调用,都会自动跳转到继承自InvocationHandler的类的invoke()方法

invoke(Object proxy, Method method, Object[] args)
Processes a method invocation on a proxy instance and returns the result

return method.invoke(target, args);

例子:

public class VectorProxy implements InvocationHandler
{
private Object proxyObj;
public VectorProxy( Object obj)
{
this.proxyObj = obj;
}

public static Object factory(Object obj)
{
Class<?> classType = obj.getClass();
return Proxy.newProxyInstance(classType.getClassLoader(), 
classType.getInterfaces(), new VectorProxy(obj));
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
    {
System.out.println("before calling: " + method);

if(null !=args)
{
for(Object obj : args)
{
System.out.println(obj);
}
}
Object object = method.invoke(proxyObj, args);

System.out.println("after calling: " + method);

return object;
    }

public static void main(String[] args)
{
List v = (List)factory(new Vector());
System.out.println(v.getClass().getName());

v.add("New");
v.add("York");
System.out.println(v);
v.remove(0);
System.out.println(v);
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值