天天看點

建立動态類的執行個體對象及調用方法

在代理類文中的ProxyTestII.java中建立動态類并調用。

方法一

package com.itheima.shipin.daili;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;

public class ProxyTestII {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Class clazzProxy = Proxy.getProxyClass(
				Collection.class.getClassLoader(), Collection.class);
		Constructor constructor = clazzProxy
				.getConstructor(InvocationHandler.class);

		class MyinvocationHanderTest implements InvocationHandler {

			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				System.out.println("My MyinvocationHanderTest ");
				return null;
			}

		}

		Collection proxy1 = (Collection) constructor
				.newInstance(new MyinvocationHanderTest());

		proxy1.clear();

	}

}
           

運作結果

My MyinvocationHanderTest

方法二

package com.itheima.shipin.daili;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;

public class ProxyTestII {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Class clazzProxy = Proxy.getProxyClass(
				Collection.class.getClassLoader(), Collection.class);
		Constructor constructor = clazzProxy
				.getConstructor(InvocationHandler.class);

		
		Collection proxy2=(Collection) constructor.newInstance(new InvocationHandler(){

			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				System.out.println("constructor.newInstance");
				return null;
			}
			
		});
		proxy2.clear();

	}
}
           

運作結果

constructor.newInstance

InvocationHandler

是代理執行個體的調用處理程式實作的接口。 

Object invoke(Object proxy,
              Method method,
              Object[] args)
              throws Throwable
           

在代理執行個體上處理方法調用并傳回結果。

方法三

package com.itheima.shipin.daili;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;

public class ProxyTestII {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
	
		Collection proxy3 = (Collection)Proxy.newProxyInstance(         //匿名内部類
				Collection.class.getClassLoader(),
				new Class[]{Collection.class},
				new InvocationHandler() {
					
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
						System.out.println("proxy.newProxyInstance");
						return null;
					}
				});
		proxy3.clear();
		

	}
}
           

運作結果

proxy.newProxyInstance

newProxyInstance傳回一個指定接口的代理類執行個體,該接口可以将方法調用指派到指定的調用處理程式。

public static Object newProxyInstance(ClassLoader loader,
                                      Class<?>[] interfaces,
                                      InvocationHandler h)
                               throws IllegalArgumentException
           

繼續閱讀