zoukankan      html  css  js  c++  java
  • Cglib的使用方法(4)BeanCopier

    cglib系列文章索引

    1. Cglib的使用方法(1)--Enhancer
    2. Cglib的使用方法(2)--CallbackFilter
    3. Cglib的使用方法(3)--Mixin
    4. Cglib的使用方法(4)--BeanCopier

    用来对象之间拷贝属性

    import net.sf.cglib.beans.BeanCopier;
    
    
    public class PropertyCopyDemo {
    	public static void main(String[] args) {
    		Other other = new Other("test", "1234");
    		Myth myth = new Myth();
    		
    		System.out.println(other);
    		System.out.println(myth);
    		
    		BeanCopier copier = BeanCopier.create(Other.class, Myth.class, false);
    		copier.copy(other, myth, null);
    		
    		System.out.println(other);
    		System.out.println(myth);
    	}
    }
    
    class Other {
    	private String username;
    	private String password;
    	private int age;
    	
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public Other(String username, String password) {
    		super();
    		this.username = username;
    		this.password = password;
    	}
    	
    	@Override
    	public String toString() {
    		return "Other: " + username + ", " + password + ", " + age;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    }
    
    class Myth {
    	private String username;
    	private String password;
    	private String remark;
    	
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	@Override
    	public String toString() {
    		return "Myth: " + username + ", " + password + ", " + remark;
    	}
    
    	public void setRemark(String remark) {
    		this.remark = remark;
    	}
    
    	public String getRemark() {
    		return remark;
    	}
    }
    

    运行结果如下:

    Other: test, 1234, 0
    Myth: null, null, null
    Other: test, 1234, 0
    Myth: test, 1234, null
    
  • 相关阅读:
    ble_app_hrs心率程序 nrf51822
    2019.05.08 《Linux驱动开发入门与实战》
    函数指针
    typedef
    回调函数
    android2
    android1
    每周总结2
    HTML
    数组(续)
  • 原文地址:https://www.cnblogs.com/icejoywoo/p/2072975.html
Copyright © 2011-2022 走看看