zoukankan      html  css  js  c++  java
  • java将父类所有的属性COPY到子类中

    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<ff.length;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;  
        }  
    
    }
  • 相关阅读:
    linux & centos命令
    javascript总结
    SocanCode7之模板编写
    SocanCode连接Oracle的方法
    ashx的使用
    SocanCode代码生成器版本更新记录 [SocanCode7全新发布]
    IIS7.0中使用MVC3,静态页正常,其它404
    不用再纠结反射影响效率了
    ASP.NET MVC 框架处理请求生命周期
    create xmlhttprequest
  • 原文地址:https://www.cnblogs.com/chasewade/p/3494312.html
Copyright © 2011-2022 走看看