网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

避免繁复的get/set操作

http://www.diybl.com/ 2007-2-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】


                      避免繁复的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();


如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言