黑马程序员——通过反射Proxy类的静态函数getProxyClass()来获取动态生成类的构造函数和方法

 ------- android培训
代码演示:
public class ProxyTest {

	public static void main(String[] args) {
		//获取类Proxy 字节码
		Class clszz = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
		
		System.out.println(clszz.getName());
		
		Constructor[] constructors = clszz.getConstructors();
		
		//打印构造函数
		for(Constructor constructor : constructors){
			String name = constructor.getName();
			StringBuilder sb = new StringBuilder(name);
			sb.append('(');
			Class[] classParams = constructor.getParameterTypes();
			
			for(Class classParam : classParams){
				sb.append(classParam.getName()).append(',');
			}
			if(classParams.length != 0){
				sb.deleteCharAt(sb.length() - 1);
			}
			
			sb.append(')');
			
			System.out.println(sb);
		}
		
		//获取类类的方法
		Method[] methods = clszz.getMethods();
		for(Method method : methods){
			String name = method.getName();
			StringBuilder sb = new StringBuilder(name);
			sb.append('(');
			Class[] classParams = method.getParameterTypes();
			
			for(Class classParam : classParams){
				sb.append(classParam.getName()).append(',');
			}
			if(classParams.length != 0){
				sb.deleteCharAt(sb.length() - 1);
			}
			
			sb.append(')');
			
			System.out.println(sb);
		}
	}

}


 java培训、期待与您交流! ----------

 
 

你可能感兴趣的:(黑马程序员——通过反射Proxy类的静态函数getProxyClass()来获取动态生成类的构造函数和方法)