My Java Learning notes (16) about object cloning and recall

Source: Internet
Author: User

1. When copying a variable, the original variable references the same object as the copy variable, and changing the object referenced by one variable will have an effect on the other variable.

A a1 = new A (); a a2 = A1;a2.up (ten);//a1 will change.

2. If you create a new copy of an object whose state is the same as the original object, but you can change the state individually later, you need to use the Clone method.

A a1 = new A (); a a2 = A1.clone (); a2.up;//a1 will not change.

3. If all data fields in an object are numeric or basic type, such a copy field has no problem, but if the object contains a reference to the child object, the result of the copy causes two fields to refer to the same child object, so the original object shares this information with the cloned object.

4. The default clone operation is a shallow copy, and it does not clone the inner object contained in the object.

5. If an object needs to be cloned without implementing the Cloneable interface, it will produce one to verify the exception.

6. Even if the default implementation of clone is a shallow copy that satisfies the requirements, the Cloneable interface should be implemented, the clone is redefined as public, and Super.clone () is called.

7. The Clone method of the object class throws an Clonenotsupportexception exception as long as there is no object in clone that implements the Cloneable interface.

Instance Code

Test class

public class Test {public static void main (string[] args) {try{employee exp = new Employee ("W1", +); Exp.sethireday (2014, 4, 2); Employee copy = Exp.clone (); Copy.raisesalary () Copy.sethireday (2014, 3, 2); SYSTEM.OUT.PRINTLN ("exp =" + exp); System.out.println ("copy =" + copy);} catch (Clonenotsupportedexception e) {e.printstacktrace ();}}}

Employee Class

Import java.util.*;p Ublic class Employee implements cloneable {private String name;private double salary;private Date Hire Day;public Employee (String n,double s) {name = N;salary = S;hireday = new Date ();} Public Employee Clone () throws Clonenotsupportedexception{employee cloned = (Employee) super.clone (); cloned.hireday = ( Date) Hireday.clone (); return cloned;} public void Sethireday (Int. year,int Month,int Day) {Date newhireday = new GregorianCalendar (year,month-1,day). GetTime (); Hireday.settime (Newhireday.gettime ());} public void Raisesalary (double p) {Double raise = salary * p/100;salary + = Raise;} Public String toString () {return "employee[name =" + name + ", salary =" + Salary + ", Hireday =" + Hireday + "]";}}


8. Recall is a common programming pattern in which the action to be taken when a feature event occurs is indicated.

Instance Code

Test class

Import Java.awt.event.actionlistener;import Javax.swing.*;import Javax.swing.timer;public class Test {public static void Main (string[] args) {ActionListener li = new Timeprinter (); Timer T = new timer (10000,li); T.start (); Joptionpane.showmessagedialog (null, "Exit?"); System.exit (0);}}

function Class

Import java.awt.*;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.util.*;p ublic Class Timeprinter implements Actionlistener{public void Actionperformed (ActionEvent event) {Date now = new Date (); System.out.println ("Now Time is:"); Toolkit.getdefaulttoolkit (). Beep ();}}

Results


My Java Learning notes (16) about object cloning and recall

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.