通过值而不是引用将对象属性复制到地图 [英] Copy Object Properties to a Map by Value not by Reference

查看:104
本文介绍了通过值而不是引用将对象属性复制到地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道自己出错的地方,但似乎我无法从对象实例中复制属性,并将它们分配给映射,但保存实例后值不会更改。



这是一个示例类:

  class Product {
String productName
字符串proudctDescription
int quantityOnHand
}

被提交并发送到我的控制器,我可以访问这些值并从可从实例获得的 productInstance.properties 映射中对它们进行操作。我想将属性复制到另一个地图以在编辑过程中提交它们之前保留这些值。所以我们假设我们正在编辑一条记录,这些记录存储在数据库中: productName =My Product, productDescription =我的产品描述 quantityOnHand = 100



我想将它们复制到:

  def propertiesBefore = productInstance .properties 

这不起作用,因为当我保存productInstance时,propertiesBefore中的值更改为任意

所以我试过这个:

  productInstance。 properties.each {k,v  - > propertiesBefore [k] = v} 

同样的事情再次发生。我不知道如何按照价值进行复制,似乎不管我尝试按照引用复制。



编辑



根据Pawel P.的要求,这是我测试的代码:

  class Product {
String productName
String productDescription
int quantityOnHand
}

def productInstance = new Product(productName:Some name,productDescription :Desciption,quantityOnHand:10)

def propertiesBefore = [:]
productInstance.properties.each {k,v - > properties [before] [k] =(v instanceof Cloneable)? v.clone():v}

productInstance.productName =x
productInstance.productDescription =y
productInstance.quantityOnHand = 9

println propertiesBefore.quantityOnHand //这将打印与save()后相同的内容
productInstance.save(flush:true)
println propertiesBefore.quantityOnHand //这将打印与在save()之上


解决方案

将[]]的值映射到一个新的哈希映射[:]的空间也可以通过推第一个哈希映射来完成,这将实现与您所期望的相同结果(按值复制)!

  def APE = [:] 
APE = [tail:1,body:hairy,hungry:Very! !]

def CAVEMAN = [:]
CAVEMAN<< APE //将APE推送到CAVEMAN的空间

//修改CAEEMAN的APE值
CAVEMAN.tail = 0
CAVEMAN.body =需要衣服

println'APE':$ {APE}
println'CAVEMAN':$ {CAVEMAN}

输出==>

 'APE':[tail:1,body:hairy,hungry:非常!!!] 
'CAVEMAN':[尾巴:0,身体:需要衣服,饥饿:非常!!!]


I'm not sure where i'm going wrong, but it seems that I'm not able to copy properties from an object instance and assign them to a map without the values being changed after saving the instance.

This is a sample class:

class Product {
    String productName
    String proudctDescription
    int quantityOnHand
}

Once the form is submitted and it's sent to my controller, I can access the values and manipulate them from the productInstance.properties map that is available from the instance. I want to copy the properties to another map to preserve the values before committing them during an edit. So let's say we are editing a record and these are the values stored in the db: productName = "My Product", productDescription = "My Product Description" and quantityOnHand = 100.

I want to copy them to:

def propertiesBefore = productInstance.properties

This did not work, because when I save the productInstance, the values in propertiesBefore change to whatever the instance had.

So I tried this:

productInstance.properties.each { k,v -> propertiesBefore[k] = v }

Same thing happened again. I am not sure how to copy by value, it seems no matter what I try it copies by reference instead.

EDIT

As per the request of Pawel P., this is the code that I tested:

class Product {
    String productName
    String productDescription
    int quantityOnHand
}

def productInstance = new Product(productName: "Some name", productDescription: "Desciption", quantityOnHand: 10)

def propertiesBefore = [:]
productInstance.properties.each { k,v -> propertiesBefore[k] = (v instanceof Cloneable) ? v.clone() : v }

productInstance.productName = "x"
productInstance.productDescription = "y"
productInstance.quantityOnHand = 9

println propertiesBefore.quantityOnHand // this will print the same as the one after the save() 
productInstance.save(flush:true)    
println propertiesBefore.quantityOnHand // this will print the same as the one above the save()

解决方案

Without cloning, copying hash-map [:]'s values to a new hash-map [:]'s space can also be done by "pushing" the first one over, which would achieve the same result that you desired (copy by value)!

def APE = [:]
APE= [tail: 1, body: "hairy", hungry: "VERY!!!"]

def CAVEMAN = [:]
CAVEMAN << APE  //push APE to CAVEMAN's space

//modify APE's values for CAVEMAN
CAVEMAN.tail = 0
CAVEMAN.body = "need clothes"

println "'APE': ${APE}"
println "'CAVEMAN': ${CAVEMAN}"

Output ==>

'APE': [tail:1, body:hairy, hungry:VERY!!!]
'CAVEMAN': [tail:0, body:need clothes, hungry:VERY!!!]

这篇关于通过值而不是引用将对象属性复制到地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆