1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 构造函数和复制函数java_用Java复制构造函数

构造函数和复制函数java_用Java复制构造函数

时间:2024-04-14 14:41:12

相关推荐

构造函数和复制函数java_用Java复制构造函数

构造函数和复制函数java

Copy Constructor in java class is a special type of constructor that takes same class as argument. Copy constructor is used to provide a copy of the specified object.

java类中的Copy构造函数是一种特殊的构造函数类型,它使用相同的类作为参数。 复制构造函数用于提供指定对象的副本。

用Java复制构造函数 (Copy Constructor in Java)

Copy constructor is an easy alternative to java cloning mechanism.

复制构造函数是Java克隆机制的简单替代方案。

Copy constructor is helpful when we want to copy an object that is heavy to instantiate. While writing copy constructor it’s very important to perform deep copy so that both the objects are detached. However in some cases where you don’t mind the change in object data, then you can also go for shallow copy.

当我们要复制要实例化的对象时,复制构造函数很有用。 在编写副本构造函数时,执行深度复制非常重要,这样两个对象都可以分离。 但是,在某些情况下,您不介意对象数据的更改,那么您也可以进行浅表复制。

Let’s see how to properly write a copy constructor in java and perform deep copy of the object.

让我们看看如何在Java中正确编写一个复制构造函数并执行对象的深层复制。

package com.journaldev.copyconstructor;import java.util.ArrayList;import java.util.List;import lombok.Data;import lombok.NoArgsConstructor;import lombok.ToString;@Data@NoArgsConstructor@ToStringpublic class State {private List<String> cities;private String name;private String country;public State(State st) {this.name = st.name; //string is immutable, so we can do direct assignmentthis.country = st.country;List<String> ct = new ArrayList<>();for (String c : st.cities) {ct.add(c);}this.cities = ct;}}

Note that I am using project lombok so that I don’t have to write boiler-plate code.

请注意,我使用的是lombok项目,因此不必编写样板代码。

Here is a simple test class where we are using copy constructor.

这是一个使用复制构造函数的简单测试类。

package com.journaldev.copyconstructor;import java.util.ArrayList;import java.util.List;public class StateTest {public static void main(String[] args) {State state = getState();State stateCopy = new State(state);System.out.println("State = "+state);System.out.println("StateCopy = "+stateCopy);stateCopy.getCities().add("Cupertino");stateCopy.setCountry("United States of America");System.out.println("State = "+state);System.out.println("StateCopy = "+stateCopy);}private static State getState() {//in real life, it will do some DB call or expensive API//class to fetch the dataState state = new State();state.setName("California");state.setCountry("USA");List<String> cities = new ArrayList<>();cities.add("San Jose"); cities.add("San Francisco");state.setCities(cities);return state;}}

Notice that I am changing some properties in one of the object, if our deep copy implementation is correctly done then it shouldn’t affect the other object. Let’s run the above program and check the output.

请注意,我正在更改一个对象中的某些属性,如果我们的深层复制实现正确完成,那么它不会影响另一个对象。 让我们运行上面的程序并检查输出。

State = State(cities=[San Jose, San Francisco], name=California, country=USA)StateCopy = State(cities=[San Jose, San Francisco], name=California, country=USA)State = State(cities=[San Jose, San Francisco], name=California, country=USA)StateCopy = State(cities=[San Jose, San Francisco, Cupertino], name=California, country=United States of America)

It’s clear from the output that our copy constructor implementation is correct, change in one of the object didn’t affected the other object and our deep copy implementation is correct.

从输出中很明显,我们的复制构造函数实现是正确的,其中一个对象的更改未影响另一个对象,而我们的深层复制实现是正确的。

翻译自: /21078/copy-constructor-in-java

构造函数和复制函数java

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。