通过实例干掉所有反射问题,有反射问题的可以来看看- package com.heima.reflection;
- 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;
- public class Demo1_Reflection {
- private String username;
- private String password;
- private int[] age;
-
- /**
- * 设置username
- * @param username username
- */
- public void setUsername(String username) {
- this.username = username;
- }
-
- /**
- * 设置password
- * @param password password
- */
- private void setPassword(String password) {
- this.password = password;
- }
-
- public static void Test01() throws ClassNotFoundException{
- Class c1 = Demo1_Reflection.class;
- Class c2 = Class.forName("com.heima.reflection.Demo1_Reflection");
-
- //获取指定包名
- String package01 = c1.getPackage().getName();
- String package02 = c2.getPackage().getName();
- System.out.println("package01 = " + package01);
- System.out.println("package02 = " + package02);
-
- //获取类的修饰符
- int mod = c1.getModifiers();
- String modifier = Modifier.toString(mod);
- System.out.println("modifier = " + modifier);
- //获取指定类的完全限定名
- String className = c1.getName();
- System.out.println("className = " + className);
- //获取指定类的父类
- Class superClazz = c1.getSuperclass();
- String superClazzName = superClazz.getName();
- System.out.println("superClazzName = " + superClazzName);
- //获取实现类的接口
- Class[] interfaces = c1.getInterfaces();
- for (Class class1 : interfaces) {
- System.out.println("interfacesName =" + class1.getName());
- }
- //获取指定类的成员变量
- Field[] fields = c1.getDeclaredFields();
- for (Field field : fields) {
- modifier = Modifier.toString(field.getModifiers());
-
- //获取字段的数据类型对应的Class对象
- Class type = field.getType();
- //获取字段名
- String name = field.getName();
- //如果是数组类型需要特别处理
- if(type.isArray()){
- String arrType = type.getComponentType().getName() + "[]";
- System.out.println(" " + modifier + " " + arrType + " " + name + ";");
- }else {
- System.out.println("" + modifier + " " + type + " " + name + ";");
- }
- }
-
- //获取类的构造方法
- Constructor[] constructors = c1.getDeclaredConstructors();
- for (Constructor constructor : constructors) {
- //构造方法名
- String name = constructor.getName();
- //获取访问修饰符
- modifier = Modifier.toString(constructor.getModifiers());
- System.out.println("" + modifier + " " + name + "(");
- //获取构造方法中的参数
- Class[] paramTypes = constructor.getParameterTypes();
- for(int i = 0;i < paramTypes.length;i++){
- if(i > 0){
- System.out.println(",");
- }
- if(paramTypes[i].isArray()){
- System.out.println(paramTypes[i].getComponentType().getName() + "[]");
- }else{
- System.out.println(paramTypes[i].getName());
- }
- }
- System.out.println(")");
- }
- //获取成员方法
- Method[] methods = c1.getDeclaredMethods();
- for (Method method : methods) {
- //获取方法的修饰符
- modifier = Modifier.toString(method.getModifiers());
- //获取方法的返回值类型
- Class returnType = method.getReturnType();
- if(returnType.isArray()){
- String arrType = returnType.getComponentType().getName()+"[]";
- System.out.println("" + modifier + " " + arrType + " " + method.getName() + "(");
- }else {
- System.out.println("" + modifier + " " + returnType.getName() + " " + method.getName() + "(");
- }
- //获取参数列表
- Class[] paramTypes = method.getParameterTypes();
- for(int i = 0;i < paramTypes.length;i++) {
- if(i > 0) {
- System.out.println(",");
- }
- if(paramTypes[i].isArray()){
- System.out.println(paramTypes[i].getComponentType().getName() + "[]");
- }else{
- System.out.println(paramTypes[i].getName());
- }
- }
- System.out.println(")");
- }
-
- }
-
- public static void Test02() throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
- //public Object invoke(Object obj,Object...args)
- //第一个参数代表对象
- //第二个参数代表执行方法上的参数
- //若反射要调用类的某个私有方法,可以在这个私有方法对应的Mehtod对象上先调用setAccessible(true)
-
- Class c1 = Demo1_Reflection.class;
- //利用反射来创建类的对象
- Demo1_Reflection d1 = (Demo1_Reflection)c1.newInstance();
- System.out.println("username ==" + d1.username);
- System.out.println("password ==" + d1.password);
- //给成员变量赋值
- Method method = c1.getDeclaredMethod("setUsername", String.class);
- method.invoke(d1, "java反射学习");
- System.out.println("username==" + d1.username);
- method = c1.getDeclaredMethod("setPassword", String.class);
- method.invoke(d1, "咪娜");
- System.out.println("password ==" + d1.password);
- //调用私有方法
- //method.setAccessible(true);
- //method.invoke(d1, "反射执行某个Private修饰的方法");
-
-
-
-
- }
-
- public static void main(String[] args) throws Exception {
- Test01();
- Test02();
- }
-
- }
复制代码 |
|