软件编程
位置:首页>> 软件编程>> java编程>> 列举java语言中反射的常用方法及实例代码

列举java语言中反射的常用方法及实例代码

作者:张星烁  发布时间:2022-10-31 13:45:07 

标签:java,反射,常用方法

Java反射机制

一、什么是反射机制 

        简单的来说,反射机制指的是程序在运行时能够获取自身的信息。在java中,只要给定类的名字,
    那么就可以通过反射机制来获得类的所有信息。

二、哪里用到反射机制 

        有些时候,我们用过一些知识,但是并不知道它的专业术语是什么,在刚刚学jdbc时用过一行代码,
    Class.forName("com.mysql.jdbc.Driver.class").newInstance();但是那时候只知道那行代码是生成
    驱动对象实例,并不知道它的具体含义。听了反射机制这节课后,才知道,原来这就是反射,现在很多开 

    框架都用到反射机制,hibernate、struts都是用反射机制实现的。

三、反射机制的优点与缺点 

        为什么要用反射机制?直接创建对象不就可以了吗,这就涉及到了动态与静态的概念, 

    静态编译:在编译时确定类型,绑定对象,即通过。 

    动态编译:运行时确定类型,绑定对象。动态编译最大限度发挥了java的灵活性,体现了多
    态的应用,有以降低类之间的藕合性。 

    一句话,反射机制的优点就是可以实现动态创建对象和编译,体现出很大的灵活性,特别是在J2EE的开发中它的灵活性就表现的十分明显。比如,一个大型的软件,不可能一次就把把它设计的很完美,当这个程序编译后,发布了,当发现需要更新某些功能时,我们不可能要用户把以前的卸载,再重新安装新的版本,假如这样的话,这个软件肯定是没有多少人用的。采用静态的话,需要把整个程序重新编译一次才可以实现功能的更新,而采用反射机制的话,它就可以不用卸载,只需要在运行时才动态的创建和编译,就可以实现该功能。 

     它的缺点是对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且它
    满足我们的要求。这类操作总是慢于只直接执行相同的操作。

列举java语言中反射的常用方法


package review;/*12:43 2019/7/21*/
import model.AnotherClass;
import model.OneClassMore;
import model.SomeClass;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 这个类列举了java语言中关于反射机制的常用的一些方法
* @author zhangxingshuo
*/
public class AboutReflection {
 public static void main(String[] args) throws Exception {
 }
 /*获得Class对象的3种方式*/
 private static Class<?> getClazz0(String className) throws ClassNotFoundException {
   Class clazz=Class.forName(className);
   return clazz;
 }
 private static Class<?> getClazz1(Object object) {
   Class clazz=object.getClass();
   return clazz;
 }
 private static Class<?> getClazz2() {
   Class clazz=model.SomeClass.class;
   return clazz;
 }
 /*经常使用的Class对象的3个方法*/
 private static String useClazz0(Class clazz) {
   String fullyQualifiedName=clazz.getName();
   return fullyQualifiedName;
 }
 private static String useClazz1(Class clazz) {
   String className=clazz.getSimpleName();
   return className;
 }                          //ex:private      //ex:abstract
 private static Object useClazz2(Class clazz) throws IllegalAccessException, InstantiationException {
   Object object=clazz.newInstance();
   return object;
 }
 /*获得Constructor对象的3个方法*/
 private static Constructor<?>[] getConstructorObject0(Class clazz) {
   Constructor<?>[] constructors=clazz.getConstructors();
   return constructors;
 }
 private static Constructor<?>[] getConstructorObject1(Class clazz) {
   Constructor<?>[] constructors=clazz.getDeclaredConstructors();
   return constructors;
 }
 private static Constructor<?> getConstructorObject2(Class clazz) throws NoSuchMethodException {
   Constructor<?> constructor=clazz.getConstructor(SomeClass.class, AnotherClass.class, OneClassMore.class);
   return constructor;
 }
 private static Constructor<?> getConstructorObject3(Class clazz) throws NoSuchMethodException {
   Constructor<?> constructor=clazz.getDeclaredConstructor(SomeClass.class, AnotherClass.class, OneClassMore.class);
   return constructor;
 }
 /*经常使用的Constructor对象的2个方法*/
 private static Object useConstructorObject0(Constructor<?> constructor) throws IllegalAccessException, InvocationTargetException, InstantiationException {
               //under here,if the variable override==true,jvm willl not check the accessible modifier
   Object object=constructor.newInstance(new SomeClass(),new AnotherClass(),new OneClassMore());
   return object;
 }
 private static void useConstructorObject1(Constructor<?> constructor) {
               //under here changing "override" variable's value who is defined in AccessibleObject,the "super and super" Class of Constructor
   constructor.setAccessible(true);
 }
 /*还有一些*/
 private static Class<?> useConstructorObject2(Constructor<?> constructor) {
   Class clazz=constructor.getDeclaringClass();
   return clazz;
 }
 private static int useConstructorObject3(Constructor<?> constructor) {
   int modifiers=constructor.getModifiers();
   return modifiers;
 }
 private static String useConstructorObject4(Constructor<?> constructor) {
      //constructor name is same as the class name
   String constructorName = constructor.getName();
                     //under here getDeclaringClass().getName();
   return constructorName;
 }
 /*获取Field对象的4个方法*/
 private static Field[] getFieldObject0(Class clazz){
   Field[] fields = clazz.getFields();
   return fields;
 }
 private static Field[] getFieldObject1(Class clazz){
   Field[] declaredFields = clazz.getDeclaredFields();
   return declaredFields;
 }
 private static Field getFieldObject2(Class clazz) throws NoSuchFieldException {
   Field field = clazz.getField("theFieldName");
   return field;
 }
 private static Field getField3(Class clazz) throws NoSuchFieldException {
   Field field = clazz.getDeclaredField("theFieldName");
   return field;
 }
 /*经常使用的Field对象的3个方法*/
 private static Object useFieldObject0(Field field,Object object) throws IllegalAccessException {
   Object fieldValue = field.get(object);
   return fieldValue;
 }
 private static void useFieldObject1(Field field,Object object) throws IllegalAccessException {
           //an object as the field value
   field.set(object,new Object());
 }
 private static void useFieldObject2(Field field){
            //same process
   field.setAccessible(true);
 }
 /*还有一些*/
 private static int useFieldObject3(Field field){
   int modifiers = field.getModifiers();
   return modifiers;
 }
 private static String useFieldObject4(Field field){
   String fieldName = field.getName();
   return fieldName;
 }
 /*获取Method对象的4个方法*/
 private static Method[] getMethodObject0(Class clazz){
   Method[] methods=clazz.getMethods();
   return methods;
 }
 private static Method[] getMethodObject1(Class clazz){
   Method[] methods=clazz.getDeclaredMethods();
   return methods;
 }
 private static Method getMethodObject2(Class clazz) throws NoSuchMethodException {
   Method method=clazz.getMethod("someMethodName",SomeClass.class,AnotherClass.class,OneClassMore.class);
   return method;
 }
 private static Method getMethodObject3(Class clazz) throws NoSuchMethodException {
   Method method=clazz.getDeclaredMethod("someMethodName",SomeClass.class,AnotherClass.class,OneClassMore.class);
   return method;
 }
 /*经常使用的Field对象的2个方法*/
 private static Object useMethodObject0(Method method,Object object) throws InvocationTargetException, IllegalAccessException {
   Object returnedobject=method.invoke(object,new SomeClass(),new AnotherClass(),new OneClassMore());
   return returnedobject;
 }
 private static void useMethodObject1(Method method){
   method.setAccessible(true);
 }
 /*还有一些*/
 private static int useMethodObject2(Method method){
   int modifiers = method.getModifiers();
   return modifiers;
 }
 private static String useMethodObject3(Method method){
   String methodName = method.getName();
   return methodName;
 }
 /*
 tips
 通过getMethods(),得到该类或接口独有的和继承自它的所有父类与接口的public方法组成的数组.
 通过getDeclaredMethods(),得到该类或接口独有的所有方法,(包括public和非public).
 */
 /*just as a empty template for convenience*/
 private static void m(){
 }
}

总结

以上所述是小编给大家介绍的列举java语言中反射的常用方法及实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

来源:https://www.cnblogs.com/zhang-xing-shuo/archive/2019/07/21/11220850.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com