反射

作者: 资本家大恶人 | 来源:发表于2020-08-05 23:33 被阅读0次

常用方法

//指定的类名返回类
Class forName(String className)//包名

Object newInstance (Object params)//将params参数传到构造器得到该类新的实体

printStackTrace()//将Throwable 对象和堆栈轨迹打印到标准错误流

//找到与类同一位置的资源,返回一个可以用来加载资源的URL或者输入流

URL getResource(Stirng name)

InputStream getRescourceAsStream(Stirng name)

Field

//返回包含Field对象数组,这些对象对应的是类中何其超类所有公共的字段并且可以得到类中所有的字段名字和值
Field[] getFields(“里面可填字段值的参数”)//填入参数即为指定
//返回包含Field对象数组,这些对象对应的是类和其超类所有的字段
Field[] getDeclaredFields(“里面可填字段值的参数”)“里面可填字段值的参数”

Object get(Object obj) //返回一个Field对象用该对象描述字段的值

Object set(Object obj,Object newValue) //返回一个Field对象用该对象设置字段的值

Method
//是该类和其超类所有的公共方法
getMethod[] getMethods()
//是该类和其超类所有的方法
getMethod[] getDeclaredMethods()
//得到方法
Method getMethod(“方法名”String name,方法的参数类型 Class  parameterTypes)

//是该类和其超类所有的公共构造方法
Constructor[] getConstructors()
是该类和其超类所有的构造方法
Constructor[] getDeclaredConstructors()

//得到该类的包名
String getPackageName()

//返回一个class对象数组
Class[] getExceptionTypes()

//返回一个整数,描述这个构造器、方法、字段的修饰符
int getModeifiers()

//返回表示一个构造起名、方法名或字段名、字符串
String getName()

//返回一个Class对象数组,其中各个对象表示参数类型
Class getParamtereTypes()

//返回一个返回类型的Class对象
Class getReturnType()

//设置可以访问private的字段,方法
setAccessible(ture or false)

// 传入参数返回具体值
Object invoke(Object implicitParamter,Onject[]explicitParamter)//方法返回的是包装类

反射得到数组

//getComponentType() 方法确定数组是是, 
    public static Object goodCopy0f(Object a, int newLength) {
        Class cl = a.getClass();
        if (!cl.isArray()) return null;
       
        Class componentType = cl.getComponentType();
        int length = Array.getLength(a);
       
        Object newArray = Array.newInstance(componentType, newLength);
        
        System.arraycopy(a, 0, newArray, 0, Math.min(length, newLength));
        return newArray;
      
    }

java.lang.Class类的getGenericInterfaces()方法用于获取由该实体直接实现的接口的类型。该实体可以是类或接口。该方法返回由该实体直接实现的接口类型的数组。

用法:

public Type[] getGenericInterfaces()
参数:此方法不接受任何参数。

返回值:此方法返回由该实体直接实现的接口类型的数组。

下面的程序演示了getGenericInterfaces()方法。

示例1:


// Java program to demonstrate 
// getGenericInterfaces() method 
  
import java.util.*; 
  
public class Test { 
    public static void main(String[] args) 
        throws ClassNotFoundException 
    { 
  
        // returns the Class object for this class 
        Class myClass = Class.forName("Test"); 
  
        System.out.println("Class represented by myClass: "
                           + myClass.toString()); 
  
        // Get the type of interfaces of myClass 
        // using getGenericInterfaces() method 
        System.out.println( 
            "GenericInterfaces of myClass: "
            + Arrays.toString( 
                  myClass.getGenericInterfaces())); 
    } 
}

输出:

Class represented by myClass: class Test
GenericInterfaces of myClass: []

示例2:


// Java program to demonstrate 
// getGenericInterfaces() method 
  
import java.util.*; 
  
interface Arr { 
} 
  
public class Test implements Arr { 
  
    public static void main(String[] args) 
        throws ClassNotFoundException 
    { 
        // returns the Class object 
        Class myClass = Class.forName("Test"); 
  
        // Get the type of interfaces of myClass 
        // using getGenericInterfaces() method 
        System.out.println( 
            "GenericInterfaces of myClass: "
            + Arrays.toString( 
                  myClass.getGenericInterfaces())); 
    } 
}

输出:

GenericInterfaces of myClass: [interface Arr]

通过反射得到内部类

//调用内部类编译器会默认给内部类参数默认加一个外部类的参数
利用getConstructor("第一个参数外部类","第二个参数为内部类参数")

相关文章

  • Java基础之反射

    Java基础之反射 反射基本介绍 反射的使用通过反射调用属性和方法通过反射获取配置文件 反射基本介绍 Java反射...

  • 镜面反射矩阵的推导

    镜面反射是以镜面作为反射平面,实物与反射物到反射平面的距离相等,实物与反射物方向相反,所以,反射矩阵由反射平面确定...

  • reflect.go包学习_之二 指针操作提高反射性能 反射应用

    reflect.go包学习_之二 指针操作提高反射性能 反射应用 反射创建实例 反射信息、反射调用方法、反射修改值...

  • Java互联网公司-经典面试题附答案

    基础:Java 反射?反射有什么缺点?你是怎么理解反射的(为什么框架需要反射)?优点:反射具有解耦性,缺点:反射属...

  • Java反射与joor反射库的使用

    java原生反射的使用 反射构造对象 反射方法 反射字段 joor反射库的使用 github:https://gi...

  • Java反射

    什么是反射? 反射的作用? 反射性能优化?

  • 反射三定律

    反射第一定律:反射可以将interface类型变量转换成反射对象 反射第二定律:反射可以将反射对象还原成inter...

  • 反射

    1.反射是什么?反射的定义,概念 2.为什么要学反射? 3.怎么使用反射?反射的使用

  • 一周岁前做好两件事,孩子就不会语言迟缓,保证口齿伶俐

    与语言发展相关的原始反射有四个:张口反射、足跖反射、抓握反射和手拉反射,每个反射的发生、发展和整合都是次第进行的,...

  • 面试官问go反射第一弹

    目录 反射概念 reflect包 反射类型(Type)和种类(Kind) 反射类型(Type)使用 反射类型对象(...

网友评论

      本文标题:反射

      本文链接:https://www.haomeiwen.com/subject/rivirktx.html