The 1.Clone () method produces an object that must be assigned after the method has been used.
Vector v2 = (vector) v.clone ();
The 2.Clone () method is a protection type method in object, and if you create a class that requires the Clone () method, you need to write it yourself and inherit the Cloneable interface.
PackageTweleve;ImportJava.util.*;classMyObjectImplementscloneable {inti; MyObject (intII) {i=II; } PublicObject Clone () {Object Object=NULL; Try{Object=Super. Clone (); } Catch(clonenotsupportedexception e) {System.out.println ("MyObject can ' t clone"); } returnobject; } PublicString toString () {returninteger.tostring (i); }} Public classlocalcopy {StaticMyObject g (MyObject v) {V.I++; returnv; } StaticMyObject F (MyObject v) {v=(MyObject) V.clone (); V.I++; returnv; } Public Static voidMain (string[] args) {MyObject amyobject=NewMyObject (11); MyObject Bmyobject=g (Amyobject); if(Amyobject = =bmyobject) System.out.println ("A==b"); ElseSystem.out.println ("A!=b"); System.out.println ("A=" +amyobject); System.out.println ("B=" +bmyobject); MyObject CMyObject=NewMyObject (31); MyObject Dmyobject=f (cmyobject); if(CMyObject = =dmyobject) System.out.println ("C==d"); ElseSystem.out.println ("C!=d"); System.out.println ("C=" +CMyObject); System.out.println ("D=" +dmyobject); }}
3. The following steps are required if you want a class to be cloned: (1) Implementing the Cloneable interface
(2) Overwrite Clone () method
(3) Call Super.clone () in your own clone () method
(4) capturing violations in their clone ()
All the methods in the 4.String class that can modify string actually create and return a new string class, the original string class is not changed, including + and +=,stringbuffer more efficient than string manipulation strings.
PackageTweleve;ImportJava.sql.Ref;Importorg.omg.CORBA.SystemException; Public classStringer {Staticstring UpCase (string s) {returns.touppercase (); } Public Static voidMain (string[] args) {String qString=NewString ("Howdy"); System.out.println (qString); String QQ=upcase (qString); SYSTEM.OUT.PRINTLN (QQ); System.out.println (qString); }}
Java Learning Notes-12. Passing and returning objects