Java learning notes - objects and classes
A little prompt for information hidden principles in Java class design
As an attribute of the class, pay attention to a point, it is not possible to return a reference to a variable object, and if it does need, it must be cloned first. For example, Gregoriancalendar is a variable object such as Class Employee {Private Java.util.gregoriancalendar hired, public java.util.gregoriancalendar gethiiredAy () {return.com
Public void sethireday (java.util.gregoriancalendar hired) {this.hireday = HIREDAY;}}
class MyCalendar {Employee employee = new Employee (); GregorianCalendar gc = new GregorianCalendar (1976,5,22); public void run () {employee.setHireDay (gc); gc.add (Calendar.YEAR, -10); System .out.println (Employee.GethireDay (). get (Calendar.year));} What is the programmatic output above? You may think that it is 1976, but it is wrong, the result is 1966. Since the modification of GC The local variable of Employee HiredAy because they point to the same address space.
Of course, the program breaks can be changed to: Class Employee {Private Java.util.gregoriancalendar Hiredness; public java.util.gregoriancalendar gethired () {Return (Gregoriancalendar) HIREDAY.CLONE ();}
Public void designay (java.util.gregoriancalendar hired) {this.hiredar hireday.clone ();}} This must be explained here. The set and get must use the clone () method, otherwise it will not achieve effect, only this can Follow the principle of information hidden, not to appear external reference directly to modify the attribute values of the class.
"Java core technology volume I: principle" (original book fifth edition) P113 Page says, Date is an inextered object, but the test is wrong, if you use Date as a field property, you must use a clone () method, otherwise the page can It cited modify the external reference to the following program segment: class Employee {private java.util.GregorianCalendar hireDay; private java.util.Date birthday; public java.util.GregorianCalendar getHireDay () {return (GregorianCalendar) hireDay.clone (); }
Public void designay (java.util.gregoriancalendar hired) {this.hireday = (gregoriancalendar) HIREDAY.CLONE ();}
Public java.util.date getBIRTHDAY () {Return (Date) birthday.clone ();
Public void setbirdhday (java.util.date birthday) {this.birdhday = (date) birthday.clone ();}
}
class MyCalendar {Employee employee = new Employee (); GregorianCalendar gc = new GregorianCalendar (1976, 5, 22); Date date = new Date (1976,5,22); public void run () {employee.setHireDay (gc); gc.add (Calendar.Year, -10); System.ustom (Employee.Gethired (). GET (Calendar.Year)); GC = Employee.GethiredAy (); gc.add (Calendar.Year, 5) System.out.println (Employee.GethiredAy (). Get (Calendar.Year)); Employee.SetBIRTHDAY (DATE); DATE.SETYEAR (1955); System.ustom (Employee.getbirthDay (). GetYear () Date = Employee.getBIRTHDAY (); DATE.SETYEAR (1900); system.out.println (Employee.GetBIRTHDAY (). GetYear ());}} All of the above output is 1976, if you remove any clONE () Method, the output is not the case. And our goal is that hopes that the properties of the class can only be modified by the SET setter class class, and do not want to modify through other ways.
The above is a little understanding of the chapter I read this book, because the book is made by the mistake, so I will write it out, for everyone to refer, I have been in contact with this question, maybe my understanding also has a mistake, I hope everyone will propose opinion.
Zeng Qingsong Zengqingsong@sohu.com2004-2-26