博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  730 随笔 :: 0 文章 :: 323 评论 :: 347万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

原理:Java的反射能够获取属性的名称,然后通过invoke调用类的某个方法。

比如有个属性叫userName,这个类写了个方法叫getUserName,通过invoke调用getUserName这个方法。

 

代码如下

 

复制代码
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ParameterBase
{
    
/**
     * Get Class field and value Map
     * 
@return
     
*/
    
public Map<String, String> getClassInfo()
    {
        Map
<String ,String>  fieldsAndValues = new HashMap<String, String>();
        Field [] fields 
= this.getClass().getDeclaredFields();
        
for(int i=0; i< fields.length; i++)
        {
            Field f 
= fields[i];
            String value 
= getFieldValue(this ,f.getName()).toString();
            fieldsAndValues.put(f.getName(),value);
        } 
      
return fieldsAndValues;
    }  
    
    
    
    
private  String getFieldValue(Object owner, String fieldName)
    {
        
return invokeMethod(owner, fieldName,null).toString();
    }
    
    
    
/**
     * 
     * 执行某个Field的getField方法
     * 
     * 
@param owner 类
     * 
@param fieldName 类的属性名称
     * 
@param args 参数,默认为null
     * 
@return 
     
*/
    
private   Object invokeMethod(Object owner, String fieldName, Object[] args)
    {
        Class
<? extends Object> ownerClass = owner.getClass();
        
        
//fieldName -> FieldName  
        String methodName = fieldName.substring(01).toUpperCase()+ fieldName.substring(1);
        
        Method method 
= null;
        
try 
        {
            method 
= ownerClass.getMethod("get" + methodName);
        } 
        
catch (SecurityException e) 
        {

            
//e.printStackTrace();
        } 
        
catch (NoSuchMethodException e) 
        {
            
// e.printStackTrace();
            return "";
        }
        
        
//invoke getMethod
        try
        {
            
return method.invoke(owner);
        } 
        
catch (Exception e)
        {
            
return "";
        }
    }
}
复制代码

 

 写一个类User继承于ParameterBase并写上一个测试代码

复制代码
public class User extends ParameterBase
{
    String userName ;
    String passWorld;
    
public String getUserName()
    {
        
return userName;
    }
    
public void setUserName(String userName)
    {
        
this.userName = userName;
    }
    
public String getPassWorld()
    {
        
return passWorld;
    }
    
public void setPassWorld(String passWorld)
    {
        
this.passWorld = passWorld;
    }
    
    
public static void main(String[] args)
    {
        User u 
= new  User();
        u.passWorld 
= "123";
        u.userName 
= "aaaaa";
        System.out.println(u.getClassInfo().toString());
        
    }
}
复制代码

 程序输出

{passWorld=123, userName=aaaaa}
posted on   Likwo  阅读(13969)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示