程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 避免繁復的get/set操作

避免繁復的get/set操作

編輯:關於JAVA

避免繁復的get/set操作 摘自:夏昕 Hibernate_DEV_GUIDE 為避免繁復的get/set操作,你可以通過Apache Jakarta Commons Beanutils 組件提供的屬性批量復制功能(http://jakarta.apache.org/commons/beanutils/).下面的例子中,我們把user對象的所有屬性復制到anotherUser對象中:

TUser user = new TUser();

TUser anotherUser = new TUser();

user.setName("Emma");

user.setUserType(1);

try {

BeanUtils.copyPropertIEs(anotherUser,user);

System.out.println("UserName => "

+anotherUser.getName()

);

System.out.println("UserType => "

+ anotherUser.getUserType()

);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

TUser user = new TUser();

TUser anotherUser = new TUser();

user.setName("Emma");

user.setUserType(1);

try {

BeanUtils.copyPropertIEs(anotherUser,user);

System.out.println("UserName => "

+anotherUser.getName()

);

System.out.println("UserType => "

+ anotherUser.getUserType()

);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

TUser user = new TUser();

TUser anotherUser = new TUser();

user.setName("Emma");

user.setUserType(1);

try {

BeanUtils.copyPropertIEs(anotherUser,user);

System.out.println("UserName => "

+anotherUser.getName()

);

System.out.println("UserType => "

+ anotherUser.getUserType()

);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved