1. Copying objects
2. Clone () Usage
3. Comparison of object instances
////////////////////
//
/////////////////////
1. Copying objects
String str1 = "this is a string! "// Here is the copy of" Object Reference"
String str2 = new string (str1); // copy the object instance
Shallow copy: only copy the composite object itself.
Deep replication: In addition to copying the composite object itself, it also copies the object instance referenced by the composite object.
For example:
Class pupil {
Public pupil (string SnO, string name, int age ){
This. Sno = new string (SNO );
This. Name = new string (name );
This. Age = age;
}
Public String getsno (){
Return SnO;
}
Public String getname (){
Return name;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Private string SnO;
Private string name;
Private int age;
}
Public class copydemo {
Public static pupil [] shallowcopy (Pupil [] Aclass ){
Pupil [] newclass = new pupil [Aclass. Length];
// Newclass and Aclass point to the same memory
For (INT I = 0; I
Newclass [I] = Aclass [I];
Return newclass;
}
Public static pupil [] deepcopy (Pupil [] Aclass ){
Pupil [] newclass = new pupil [Aclass. Length];
// At this time, the corresponding SnO and name of newclass and Aclass point to the same memory.
For (INT I = 0; I
String SnO = Aclass [I]. getsno ();
String name = Aclass [I]. getname ();
Int age = Aclass [I]. getage ();
Newclass [I] = new pupil (SNO, name, age );
}
Return newclass;
}
Public static pupil [] deepercopy (Pupil [] Aclass ){
Pupil [] newclass = new pupil [Aclass. Length];
// Complete Replication
For (INT I = 0; I
String SnO = new string (Aclass [I]. getsno ());
String name = new string (Aclass [I]. getname ());
Int age = Aclass [I]. getage ();
Newclass [I] = new pupil (SNO, name, age );
}
Return newclass;
}
}
2. Clone () Usage
* Object. Clone ()
* Cloneable Interface
* Clonenotsupportedexception
A. Use object. Clone for copying
Two required conditions:
1. Be sure to redefine the clone () method as a public method (in the object class, it is a protected member and cannot be used directly)
2. This descendant class declares the implementation interface cloneable interface (when the class implements this interface, any subclass of the class will inherit this interface), this interface does not actually have any
The content is just an identifier, indicating that the class implementing this interface provides the clone () method (this is an atypical usage of the interface)
Public class fraction implements cloneable {
Public object clone (){
Try {
Return super. Clone (); // call protected Method
} Catch (clonenotsupportedexception e ){
Return NULL;
}
}
// Other methods...
}
B. Rewrite object. Clone ()
For example, copy private char [] CB; Character Buffer
// Add in class cirbuf
Public object clone (){
Try {
Cirbuf copy = (cirbuf) Super. Clone ();
Copy. cb = (char []) CB. Clone ();
Return copy;
} Catch (clonenotsupportedexception e ){
Throw new internalerror (E. tostring ());
}
}
C. Copy the Array
An array is an object that is passed in the form of a reference when the method call is repeated. It is very suitable for referencing to pass an array in the following cases:
* The method being received does not modify the array.
* The method being called does not have to worry about modifying the array.
* The method being called wants to get the Modification result in the array.
Otherwise, the copy of the array object should be passed in the method call. you only need to call arrobj. the clone () method can be used to copy the array arrobj. then, the array copy is forcibly converted to its correct type:
(Type []) arrobj. Clone ();
The system. arraycopy method provides an effective method for copying multiple elements between arrays.
System. arraycopy (source, I, target, J, Len)
3. Comparison of object instances
For example:
Pupil p1 = new pupil ("99184001", "zhang3", 18 );
Pupil P2 = new pupil ("99184001", "zhang3", 18 );
A. "="
If (p1 = P2 )...
In this test, the object reference is used, and the result must be false. As long as the two object references are not aliases, they will not be equal.
B. Relatively false
If (p1.getsno () = p2.getsno () & p1.getname () = p2.getname ()
& P1.getage () = p2.getage ())...;
C. Compare to true [/Code]
If (p1.getsno (). Equals (p2.getsno () & p1.getname (). Equals (p2.getname ())
& P1.getage () = p2.getage ()...; [/Code]
Java API and class objects also provide the equals () method, but it only compares two object references, not two object instances.
However, if you need to compare objects of the pupil class (for example, to put them into an object container), you should redefine the equals () method for the pupil class:
Public Boolean equals (Object otherobj ){
// Check whether otherobj is empty
If (otherobj = NULL) return false;
// Check whether otherobj is the current object
If (otherobj = This) return true;
// Check whether otherobj has the correct type, that is, whether it can be compared with the current object
If (! (Otherobj instanceof pupil) return false;
// Convert otherobj to object reference of the pupil class
Pupil tmpobj = (pupil) otherobj;
// Check whether the logic of the student is equal
If (Sno. Equals (tmpobj. SnO) & name. Equals (tmpobj. Name)
& Amp; age = tmpobj. Age) return true;
Return false;
}
Almost every class provided by Java APIs provides the equals () method that adopts a deep comparison policy, such as the string equals () method. generally, a user-defined class should also provide an appropriate equals () method, especially when the program wants to put its object into the object container class provided by the Java API.
According to the Convention, the equality comparison implemented by the equals () method provided by any class should be equivalent, that is, the equivalence, symmetry, and transmission are satisfied. another class re-defines the equals () method, and should also re-define the corresponding hashcode () method, otherwise the class object will also occur when it is put into the ing object container.