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);
}
}