这将被视为深拷贝返回吗?

huangapple go评论56阅读模式
英文:

Will this be considered a deep copy return?

问题

我创建了一个名为"User"的实体类,其中包括生成的值"Id"。

以下是代码片段:

@Transient
private User u;

public User() {
    super();
}

public User(String name, Integer age, Occupation oc, Integer salary) {
    this.name = name;
    .
    .
    .
}

// Getters
public Integer getUniqueID() {
    u = new User(this.name, this.age, this.oc, this.salary);
    return u.uniqueID;
}

public String getName() {
    u = new User(this.name, this.age, this.oc, this salary);
    return u.name;
}
.
.
.

这是否被认为是深拷贝返回?是否存在封装违规?

英文:

I created a Entity class - "User" with Generated Value - "Id".

Below is the snippet of the code:

@Transient
private User u;

public User(){
    super();
}

public User(String name, Integer age, Occupation oc, Integer salary){
    this.name = name;
    .
    .
    .
}

//Getters
    public Integer getUniqueID() {
    u = new User(this.name, this.age, this.oc, this.salary);
    return u.uniqueID;
}

public String getName() {
    u = new User(this.name, this.age, this.oc, this.salary);
    return u.name;
}
.
.
.

Will this be considered a deep copy return? Any encapsulation breach?

答案1

得分: 0

为了制作User的深度复制,您需要复制Occupation,其他字段不相关。在您的类中包括一个复制构造函数通常是一个好主意:

public User(User other){
    this(other.name, other.age, other.oc, other.salary);
}

您可以这样制作一个“深度”复制构造函数:

public User(User other){
    this(other.name,
            other.age,
            other.oc == null ? null : new Occupation(other.oc),
            other.salary);
}

或者,您可以在另一个构造函数中创建Occupation的副本:

public User(String name, Integer age, Occupation oc, Integer salary){
    this.name = name;
    this.age = age;
    this.oc = oc == null ? null : new Occupation(oc);
    this.salary = other.salary;
}

您还可以添加一个用于复制用户的方法:

public User copy() {
    return new User(name, age, oc, salary);
}

并且添加一个用于深度复制的方法:

public User deepCopy() {
    return new User(name, age, oc == null ? null : oc.deepCopy(), salary);
}

假设您在Occupation类中有一个类似的deepCopy方法。

有时在getter和setter中复制对象是一个好主意:

public Occupation getOccupation() {
    return oc == null ? null : new Occupation(oc);
}

public void setOccupation(Occupation oc) {
    this.oc = oc == null ? null : new Occupation(oc);
}

当然,复制对象会带来一些开销,有时实际上您不想复制对象,而是希望从两个或更多对象中引用相同的对象。您需要决定哪些字段需要复制,以及深度如何复制。

请注意,不可变对象永远不需要复制,应尽可能多地使用它们。

英文:

To make a deep copy of a User you would need to copy the Occupation as well; the other fields are irrelevant. It is often a good idea to include a copy constructor in your class:

public User(User other){
    this(other.name, other.age, other.oc, other.salary);
}

You can make it a "deep" copy constructor like this:

public User(User other){
    this(other.name,
            other.age,
            other.oc == null ? null : new Occupation(other.oc),
            other.salary);
}

Alternatively, you could create the copy of the Occupation in the other constructor:

public User(String name, Integer age, Occupation oc, Integer salary){
    this.name = name;
    this.age = age;
    this.oc = oc == null ? null : new Occupation(oc),
    this.salary = other.salary;
}

You can also add method to make a copy of a user:

public User copy() {
    return new User(name, age, oc, salary);
}

And and another one to make a deep copy:

public User deepCopy() {
    return new User(name, age, oc == null ? null : oc.deepCopy(), salary);
}

assuming you have a similar deepCopy method in class Occupation.

It's sometimes a good idea to copy objects in getters and setters:

public Occupation getOccupation() {
    return oc == null ? null : new Occupation(oc);
}

public void setOccupation(Occupation oc) {
    this.oc = oc == null ? null : new Occupation(oc);
}

Of course, making a copy of an object has a cost, and sometimes you actually don't want to copy objects, but rather reference the same object from two or more objects. You have to decide which fields have to be copied, and how deep.

Note that immutable objects don't ever need to be copied, and should be used as much as possible.

huangapple
  • 本文由 发表于 2023年3月9日 19:57:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定