-
用反射的方式获取父类中的所有属性和方法
- package com.syh.jdbc.reflection_super;
-
-
- public class Parent {
-
- public String publicField = "1";
-
- String defaultField = "2";
-
- protected String protectedField = "3";
-
- private String privateField = "4" ;
-
- public void publicMethod() {
- System.out.println("publicMethod...");
- }
-
- void defaultMethod() {
- System.out.println("defaultMethod...");
- }
-
- protected void protectedMethod() {
- System.out.println("protectedMethod...");
- }
-
- private void privateMethod() {
- System.out.println("privateMethod...");
- }
-
- }
- package com.syh.jdbc.reflection_super;
-
-
- public class Son extends Parent{
-
- }
- package com.syh.jdbc.reflection_super;
-
- import java.lang.reflect.Field;
-
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
-
-
- public class ReflectionUtils {
-
-
-
- public static Method getDeclaredMethod(Object object, String methodName, Class<?> ... parameterTypes){
- Method method = null ;
-
- for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
- try {
- method = clazz.getDeclaredMethod(methodName, parameterTypes) ;
- return method ;
- } catch (Exception e) {
-
-
- }
- }
-
- return null;
- }
-
-
-
- public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
- Object [] parameters) {
-
- Method method = getDeclaredMethod(object, methodName, parameterTypes) ;
-
-
- method.setAccessible(true) ;
-
- try {
- if(null != method) {
-
-
- return method.invoke(object, parameters) ;
- }
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
-
- return null;
- }
-
-
-
- public static Field getDeclaredField(Object object, String fieldName){
- Field field = null ;
-
- Class<?> clazz = object.getClass() ;
-
- for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
- try {
- field = clazz.getDeclaredField(fieldName) ;
- return field ;
- } catch (Exception e) {
-
-
- }
- }
-
- return null;
- }
-
-
-
- public static void setFieldValue(Object object, String fieldName, Object value){
-
-
- Field field = getDeclaredField(object, fieldName) ;
-
-
- field.setAccessible(true) ;
-
- try {
-
- field.set(object, value) ;
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
-
- }
-
-
-
- public static Object getFieldValue(Object object, String fieldName){
-
-
- Field field = getDeclaredField(object, fieldName) ;
-
-
- field.setAccessible(true) ;
-
- try {
-
- return field.get(object) ;
-
- } catch(Exception e) {
- e.printStackTrace() ;
- }
-
- return null;
- }
- }
- package com.syh.jdbc.reflection_super;
-
- import static org.junit.Assert.*;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
-
- import org.junit.Test;
-
-
- public class ReflectionUtilsTest {
-
-
-
- @Test
- public void testGetDeclaredMethod() {
-
- Object obj = new Son() ;
-
-
- System.out.println(publicMethod.getName());
-
-
- System.out.println(defaultMethod.getName());
-
-
- System.out.println(protectedMethod.getName());
-
-
- System.out.println(privateMethod.getName());
- }
-
-
-
- @Test
- public void testInvokeMethod() throws Exception {
- Object obj = new Son() ;
-
-
-
-
-
-
-
-
- }
-
-
-
- @Test
- public void testGetDeclaredField() {
-
- Object obj = new Son() ;
-
-
- System.out.println(publicField.getName());
-
-
- System.out.println(defaultField.getName());
-
-
- System.out.println(protectedField.getName());
-
-
- System.out.println(privateField.getName());
-
- }
-
- @Test
- public void testSetFieldValue() {
-
- Object obj = new Son() ;
-
- System.out.println("原来的各个属性的值: ");
- System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
- System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
- System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
- System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
-
- ReflectionUtils.setFieldValue(obj, "publicField", "a") ;
- ReflectionUtils.setFieldValue(obj, "defaultField", "b") ;
- ReflectionUtils.setFieldValue(obj, "protectedField", "c") ;
- ReflectionUtils.setFieldValue(obj, "privateField", "d") ;
-
- System.out.println("***********************************************************");
-
- System.out.println("将属性值改变后的各个属性值: ");
- System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
- System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
- System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
- System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
-
- }
-
- @Test
- public void testGetFieldValue() {
-
- Object obj = new Son() ;
-
- System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
- System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
- System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
- System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
- }
-
- }
-
相关阅读:
文件操作
xadmin的使用
Vue
Redis
Django
python小结
利用线程池和回调函数爬虫
drf-基表、断关联表关系、级联删除、正方向查询、子序列化
drf序列化
drf初识
-
原文地址:https://www.cnblogs.com/toSeeMyDream/p/5680007.html
Copyright © 2020-2023
润新知