反射

1.Student类

package com.pro.test;

public class Student {

	private int stId;
	private String stName;
	private int age;
	public void ff(String stId,String stName,int age) {
		System.out.println(stId+stName+age);
	}
	public int getStId() {
		return stId;
	}
	public void setStId(int stId) {
		this.stId = stId;
	}
	public String getStName() {
		return stName;
	}
	public void setStName(String stName) {
		this.stName = stName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(int stId, String stName, int age) {
		super();
		this.stId = stId;
		this.stName = stName;
		this.age = age;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

2.反射

package com.pro.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.lang.reflect.TypeVariable;

public class Test {

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		Class<?> st = Class.forName("com.pro.test.Student");//找到类
		Object obj = st.newInstance();
		Method method1=st.getDeclaredMethod("ff", String.class,String.class,int.class);
		Object obj1=method1.invoke(obj,"1","张三",18);//调用Student的方法
		
		
		StringBuffer bf=new StringBuffer();
		int classModify = st.getModifiers();
		String str = Modifier.toString(classModify);
		bf.append(str+" "+"class"+" "+st.getSimpleName()+"{");
		
		Field[] fields= st.getDeclaredFields();//反射属性
		for (Field field : fields) {
			int modifiers = field.getModifiers();
			String fieldModify = Modifier.toString(modifiers);
			Class<?> type = field.getType();
			String typeName=type.getSimpleName();
			bf.append("\n\t"+fieldModify+" "+typeName+" "+field.getName());
		}
		
		Method[] methods = st.getDeclaredMethods();//反射方法
		for (Method method : methods) {
			String methodModify=Modifier.toString(method.getModifiers());
			Class<?> methodReturnType = method.getReturnType();
			String methodReturnTypeName = methodReturnType.getSimpleName();
			bf.append("\n\t"+methodModify+" "+methodReturnTypeName+" "+method.getName()+"(");
			Parameter[] parameters = method.getParameters();
			for (Parameter parameter : parameters) {
				Class<?> type = parameter.getType();
				String parameterTypeName = type.getSimpleName();
//				String name = parameter.getName();
				bf.append(parameterTypeName+",");				
			}
			bf.append("){\n}");
		}
		
		Constructor<?>[] constructors = st.getConstructors();//反射构造函数
		for (Constructor<?> constructor : constructors) {
			String constructorModify = Modifier.toString(constructor.getModifiers());
			bf.append("\n\t"+constructorModify+" "+st.getSimpleName()+"(");
			Parameter[] parameters = constructor.getParameters();
			for (Parameter parameter : parameters) {
				Class<?> type = parameter.getType();
				String parameterTypeName = type.getSimpleName();
//				String name = parameter.getName();
				bf.append(parameterTypeName+",");
			}
			bf.append("){\n}");
		}
		System.out.println(bf);
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值