JAva親のすべての属性COPYを子に

4188 ワード

public class FatherToChildUtils {
    /* 
     *         COPY    。 
     *     child   extends father; 
     *   child father     javabean  ,   deleteDate,   getDeleteDate 
     */  
    public static void fatherToChild (Object father,Object child){  
        if(!(child.getClass().getSuperclass()==father.getClass())){  
            System.err.println("child  father   "); 
        }  
        Class fatherClass= father.getClass();  
        Field ff[]= fatherClass.getDeclaredFields();  
        for(int i=0;i){  
            Field f=ff[i];//       , deleteDate  
            Class type=f.getType();  
            try {
                Method m = fatherClass.getMethod("get"+upperHeadChar(f.getName()));//  getDeleteDate  
                Object obj=m.invoke(father);//                    
                f.set(child,obj);
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }  
    }  
    /** 
     *      ,in:deleteDate,out:DeleteDate 
     */  
    private static String upperHeadChar(String in){  
        String head=in.substring(0,1);  
        String out=head.toUpperCase()+in.substring(1,in.length());  
        return out;  
    }  

}

 
転載先:https://www.cnblogs.com/chasewade/p/3494312.html