First, let's take a look at the definition of the shortest copy and deep copy:
Shallow copy: assign values to the member variables of the newly created instance one by one using a known instance. This method is called shortest copy.
Deep copy: When a copy constructor of a class not only copies all non-referenced member variable values of an object, but also creates a new instance for referenced member variables, the instance value is initialized as a form parameter. This method is called Deep copy.
That is to say, the shortest copy only copies one object and transmits references. The instance cannot be copied. Deep COPY Copies all references to the object. It creates a new instance and copies the instance.
For the shortest copy, when the object's member variables are of the basic data type, the member variables of the two objects already have a bucket. The value assignment operation passes the value, so the shortest copy can copy instances. However, when the object's member variables are of the reference data type, the object cannot be copied.
There is an object Person. The Code is as follows:
Person(String name,String sex,.name =.sex =.age = Person(Person p){ .name =.sex =.age =
The above object Person has three member variables. Name, sex, age. Two constructor methods. The second parameter is the object, which is called the copy constructor. It initializes the new object to the Instance value of the form parameter and enables object replication.
Another object, Asian, is as follows:
.skin =.person = person; Asian(Asian asian){
The preceding object also has two member variables: skin and Person.
The person object is as follows:
Person p1 = Person ("Li Si", "mam", 23 = Person (P1 );
When the preceding statement is called. P2 object will copy P1. The execution is as follows:
For Asian objects:
Asian a1 = Asian ("yellow", Person ("Li Si", "mam", 23 = Asian (a1 );
New Asian (a1) executes the Asian class copy construction method, because the object Value assignment is a reference value assignment. So that a1 and a2 reference the same object
For example:
When a1 executes a statement that can change this value, a1 can use this statement to change the member variables of the a2 object.
If you execute the following statement: a2.name = new Person (a1.name)
A new Person object will be created.
For example: