package com.zhou.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class test03 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1=Class.forName("com.zhou.HelloWorld");
//获得类的名字
System.out.println(c1.getName());
System.out.println(c1.getSimpleName());
//获得类的属性
//找到类的共有属性
Field[] fields = c1.getFields();
//找到全部属性
fields=c1.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
//找到指定属性
Field name = c1.getDeclaredField("name");
System.out.println(name);
//打印方法
System.out.println("=======================");
//获得本类和父类的所有public方法
Method[] methods = c1.getMethods();
for (Method method : methods) {
System.out.println("getMethods "+method);
}
Method[] declaredMethods = c1.getDeclaredMethods();
//调用本类所有的方法
for (Method declaredMethod : declaredMethods) {
System.out.println("getDeclaredMethods "+declaredMethod);
}
//获得指定方法
//重载 所以需要输入参数,否则识别不到
System.out.println("=======================");
Method getName = c1.getMethod("getName");
Method getName2 = c1.getMethod("setName", String.class);
System.out.println(getName);
System.out.println(getName2);
//获得构造器
Constructor[] constructors = c1.getConstructors();
Constructor[] declaredConstructors2 = c1.getDeclaredConstructors();
}
}