• 实体类解析


    解析指定格式为对象

    /**
    	 * 解析指定格式为对象
    	 * @param string格式数据:<a>**</a><b>**</b>
    	 * @param obj 对象
    	 * @return
    	 */
    	public static Object setParamToObj(String string,Object obj){
    		String[] args=string.split("<");
    		for(int i=1; i<args.length;i++){
    			System.out.println(i+"=="+args[i]);
    			String[] arg=args[i].split(">");
    			obj=values(arg[0],arg[1],obj);
    			i++;
    		}
    		return obj;
    	}
    	/**
    	 * 设置对象对应的熟悉值
    	 */
    	private static Object values(String key,Object value,Object bean){
    		String methodName="set"+key.substring(0,1).toUpperCase()+key.substring(1);
    		System.out.println(methodName);
    		try{
    			Method[] methods = bean.getClass().getMethods();  
    	        for(Method method:methods){  
    	            //如果方法同名则执行该方法(不能用于BO中有重载方法的情况)  
    	            if(methodName.equals(method.getName())){  
    	            	//设置参数类型
    	            	Class<?>[] clazz = method.getParameterTypes();  
    	                String type = clazz[0].getName();  
    	                if(type.equals("java.lang.String")){  
    	                    method.invoke(bean,(String)value);  
    	                }  
    	                else if(type.equals("java.util.Date")){  
    	                    method.invoke(bean, (Date)value);  
    	                }  
    	                else if(type.equals("java.lang.Integer")){  
    	                    method.invoke(bean, new Integer((String)value));  
    	                }else{  
    	                	method.invoke(bean, value); 
    	                }
    	            }  
    	        } 
    		}catch (Exception e) {
    			e.printStackTrace();
    		}
    		return bean;
    	}
    

      设置对象为指定格式

    	/**
    	 * 设置对象为指定格式
    	 * @param args 属性
    	 * @param obj 对象
    	 * @return 格式数据:<a>**</a><b>**</b>
    	 */
    	public static String getParamTostring(String[] args,Object obj){
    		StringBuffer string=new  StringBuffer("");
    		if(obj!=null){
    			for(int i=0;i<args.length;i++){
    				string.append("<"+args[i]+">"+values(args[i], obj)+"</"+args[i]+">");
    			}
    		}
    		return string.toString();
    	}
    	/**
    	 * 获取对象对应的熟悉值
    	 */
    	private static String values(String key,Object bean){
    		String value="";
    		String methodName=key.substring(0,1).toUpperCase()+key.substring(1);
    		try{
    			Method method=bean.getClass().getDeclaredMethod("get"+methodName);
    			Object obj=method.invoke(bean);
    			if(obj!=null){
    				value=obj.toString();
    			}
    		}catch (NoSuchMethodException e) {
    			e.printStackTrace();
    			System.out.println("该属性在Bean中没有相应的get方法!!");
    		}catch (IllegalArgumentException e) {
    			e.printStackTrace();
    			System.out.println("方法的参数错误!!");
    		}catch (IllegalAccessException e) {
    			e.printStackTrace();
    			System.out.println("当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义!!");
    		}catch (InvocationTargetException e) {
    			e.printStackTrace();
    			System.out.println("是一种包装由调用方法或构造方法所抛出异常的经过检查的异常!!");
    		}
    		return value;
    	}
    

      测试

    	public static void main(String[] args) {
    		Test t=new Test("1","2");
    		String[] param={"a","b"};
    		String string=getParamTostring(param,t);
    		
    		Test t2=setParamToObj(string,new Test());
    		System.out.println(t2.getA());
    		
    	}
    

      

  • 相关阅读:
    Matlab实现bwlabel函数(区域标记)功能
    Matlab实现medfilt2函数功能
    Matlab实现基于频域对二维信号的低通滤波
    Matlab实现基于频域对一维信号利用傅里叶低通滤波平滑
    Matlab实现直方图规定化
    Matlab实现直方图均衡化
    Matlab实现imresize函数功能
    lc279贪心
    lc347 解法
    numpy中的np.mat(1)
  • 原文地址:https://www.cnblogs.com/tongxinyuan/p/4568684.html
Copyright © 2020-2023  润新知