Java反射怎么调用静态(私有)方法和非静态(私有)方法

您所在的位置:网站首页 java私有类如何使用 Java反射怎么调用静态(私有)方法和非静态(私有)方法

Java反射怎么调用静态(私有)方法和非静态(私有)方法

2024-07-13 09:06| 来源: 网络整理| 查看: 265

在一个类中声明一个静态方法、非静态方法、私有静态方法和私有非静态方法。

public class StaticAndNoStaticTest { public static void staticMethod(String[] args) { for (String str : args) { System.out.println(str); } } public void NoStaticMethod(String[] args) { for (String str : args) { System.out.println(str); } } private static void staticPrivateMethod(String s) { System.out.println("this is a private static method and the parameters is: " + s); } private void noStaticPrivateMethod(String s, Integer i) { System.out.println("this is a private no static method and the parameters is: " + s + i); } }

1、调用静态方法,不需要获取类对象。

// 调用静态方法,不需要获取类对象 // 获取字节码对象 Class clazz = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); String[] s = new String[]{"riemann", "chow"}; // 获取Method对象 Method staticMethod = clazz.getMethod("staticMethod", String[].class); // 调用invoke方法来调用 staticMethod.invoke(null, (Object) s);

2、调用非静态方法,需要获取类对象。

// 调用非静态方法,需要获取类对象 // 获取字节码对象 Class clazz = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); // 获取一个对象 Constructor constructor = clazz.getConstructor(); StaticAndNoStaticTest instance = constructor.newInstance(); String[] s = new String[]{"riemann", "chow"}; // 获取Method对象 Method noStaticMethod = clazz.getMethod("NoStaticMethod", String[].class); // 调用invoke方法来调用 noStaticMethod.invoke(instance, (Object) s);

3、反射调用私有静态方法

Class clazzStaticPrivate = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); Constructor constructor2 = clazzStaticPrivate.getConstructor(); StaticAndNoStaticTest instance2 = constructor2.newInstance(); Method staticPrivateMethod = clazzStaticPrivate.getDeclaredMethod("staticPrivateMethod", new Class[]{String.class}); // 强制进入 staticPrivateMethod.setAccessible(true); staticPrivateMethod.invoke(instance2, "test static and private");

4、反射调用私有非静态方法

Class clazzNoStaticPrivate = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); Constructor constructor3 = clazzNoStaticPrivate .getConstructor(); StaticAndNoStaticTest instance3 = constructor3.newInstance(); Method noStaticPrivateMethod = clazzNoStaticPrivate.getDeclaredMethod("noStaticPrivateMethod", new Class[]{String.class, Integer.class}); // 强制进入 noStaticPrivateMethod.setAccessible(true); noStaticPrivateMethod.invoke(instance3, "test no static and private ", 20190914);

输出结果都是:

==========反射调用静态方法========== riemann chow ==========反射调用非静态方法========== riemann chow ==========反射调用私有静态方法========== this is a private static method and the parameters is: test static and private ==========反射调用私有非静态方法========== this is a private no static method and the parameters is: test no static and private 20190914

getDeclaredMethod方法第一个参数是方法名,第二个是参数类型的数组

invoke方法第一个参数是类或者对象实例,后面的参数是方法形参

setAccessible要设置成true的,否则无法调用private方法

整个类的代码分享如下:

package com.reflect.staticAndNoStatic; import java.lang.reflect.Constructor; import java.lang.reflect.Method; /** * @author riemann * @date 2019/09/14 15:16 */ public class StaticAndNoStaticTest { public static void main(String[] args) throws Exception { System.out.println("==========反射调用静态方法=========="); // 反射调用静态方法,不需要获取类对象 // 获取字节码对象 Class clazzStatic = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); String[] s = new String[]{"riemann", "chow"}; // 获取Method对象 Method staticMethod = clazzStatic.getMethod("staticMethod", String[].class); // 调用invoke方法来调用 staticMethod.invoke(null, (Object) s); System.out.println("==========反射调用非静态方法=========="); // 反射调用非静态方法,需要获取类对象 // 获取字节码对象 Class clazzNoStatic = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); // 获取一个对象 Constructor constructor = clazzNoStatic.getConstructor(); StaticAndNoStaticTest instance = constructor.newInstance(); String[] ss = new String[]{"riemann", "chow"}; // 获取Method对象 Method noStaticMethod = clazzNoStatic.getMethod("NoStaticMethod", String[].class); // 调用invoke方法来调用 noStaticMethod.invoke(instance, (Object) ss); System.out.println("==========反射调用私有静态方法=========="); Class clazzStaticPrivate = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); Constructor constructor2 = clazzStaticPrivate.getConstructor(); StaticAndNoStaticTest instance2 = constructor2.newInstance(); Method staticPrivateMethod = clazzStaticPrivate.getDeclaredMethod("staticPrivateMethod", new Class[]{String.class}); // 强制进入 staticPrivateMethod.setAccessible(true); staticPrivateMethod.invoke(instance2, "test static and private"); System.out.println("==========反射调用私有非静态方法=========="); Class clazzNoStaticPrivate = (Class) Class.forName("com.reflect.staticAndNoStatic.StaticAndNoStaticTest"); Constructor constructor3 = clazzNoStaticPrivate.getConstructor(); StaticAndNoStaticTest instance3 = constructor3.newInstance(); Method noStaticPrivateMethod = clazzNoStaticPrivate.getDeclaredMethod("noStaticPrivateMethod", new Class[]{String.class, Integer.class}); // 强制进入 noStaticPrivateMethod.setAccessible(true); noStaticPrivateMethod.invoke(instance3, "test no static and private ", 20190914); } public static void staticMethod(String[] args) { for (String str : args) { System.out.println(str); } } public void NoStaticMethod(String[] args) { for (String str : args) { System.out.println(str); } } private static void staticPrivateMethod(String s) { System.out.println("this is a private static method and the parameters is: " + s); } private void noStaticPrivateMethod(String s, Integer i) { System.out.println("this is a private no static method and the parameters is: " + s + i); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3