Package com.xing.test; Public className implements cloneable{/** * @param args * Create object and object clones*/ PrivateString name; Private intAge ; PublicName () {} PublicName (String name,intAge ) { This. name=name; This. age=Age ; } PublicString toString () {return "Student Name:"+name+", Student Age:"+Age ; } Public Static voidMain (string[] args) throws Exception {System. out. println ("----------New-------------"); Name pp=NewName ("Little Two", -); System. out. println (PP); System. out. println ("creates an object----------the Clone () method of the calling object-------------"); Name PP3=(Name) Pp.clone (); System. out. println (PP3); System. out. println ("----------Call Java.lang.Class's Newinstance () method to create an object-------------"); Class QQ=class.forname ("Com.xing.test.Name"); Name pp2=(Name) qq.newinstance (); System. out. println (PP2); }}
Note:
When you create an object using the new and Java.lang.Class newinstance () methods, the constructor method of the class is called. The latter invokes the default constructor method of the class, which is the parameterless constructor method, and if the new construction method overrides the original construction method, a non-parametric construct must be re-written. When an object is created using the Clone () method of the object class, the constructor of the class is not called, and he creates a replicated object that has a different memory address than the original object, but whose property values are the same.
- Implicitly creating objects
1, String name= "hah"; "Hah" is a string object that is implicitly created by the Java Virtual machine.
2, the result of the + operator of the string is a new string object
String a1= "a";
String a2= "B";
string a3=a1+a2; A3 is a new string object
3. When a Java virtual machine loads a class, it implicitly creates a class instance that describes the class
Class loading refers to reading the binary data in the class's. class file into memory, storing it in the run-time data area, and then creating a Java.lang.Class object in the heap that encapsulates the data structure of the class within the method area.
- How to implement object cloning
- There are two ways of doing this:
?? 1). Implement the Cloneable interface and override the Clone () method in the object class;
?? 2). Implement the Serializable interface, through the serialization and deserialization of objects to achieve cloning, you can achieve true deep cloning, the code is as follows.
Package com.xing.test; Public classClonetest { Public Static voidMain (string[] args) {Try{Person1 P1=NewPerson1 ("Hao LUO", -,NewCar ("Benz", -)); Person1 P2= Myutil.clone (p1);//Deep CloningP2.getcar (). Setbrand ("BYD"); //Modify the cloned person object P2 The associated car object's branding properties//The original Person object P1 associated car will not be affected by any//because the associated car object was cloned when the person object was cloned.System. out. println (p1); } Catch(Exception e) {e.printstacktrace (); } }}
Object creation and cloning