实在抱歉,return hireDay;这个语句没有错,是我提问错了。今天我弄了4个小时,终于明白了,这句语句是没有错,但存在bug,请看以下代码,就会明白。
代码1:- import java.util.*;
- public class EmployeeDemo {
- public static void main(String[] args) {
- Employee ep=new Employee(2011,9,18);
- Date dt=ep.getHireDay();
- System.out.println(ep.getHireDay());
- double te=10*365.25*24*60*60*1000;
- dt.setTime(dt.getTime()+(long)te);
- System.out.println(ep.getHireDay());
- }
- }
- class Employee
- {
- private Date hireDay;
- public Date getHireDay()
- {
- return hireDay;
- }
- Employee(int year,int month,int day)
- {
- GregorianCalendar gc=new GregorianCalendar(year,month-1,day);
- hireDay=gc.getTime();
- }
- }
复制代码 运行结果:
Sun Sep 18 00:00:00 CST 2011
Fri Sep 17 12:00:00 CST 2021
代码2:- import java.util.*;
- public class EmployeeDemo {
- public static void main(String[] args) {
- Employee ep=new Employee(2011,9,18);
- Date dt=ep.getHireDay();
- System.out.println(ep.getHireDay());
- double te=10*365.25*24*60*60*1000;
- dt.setTime(dt.getTime()+(long)te);
- System.out.println(ep.getHireDay());
- }
- }
- class Employee
- {
- private Date hireDay;
- public Date getHireDay()
- {
- return (Date)hireDay.clone();
- }
- Employee(int year,int month,int day)
- {
- GregorianCalendar gc=new GregorianCalendar(year,month-1,day);
- hireDay=gc.getTime();
- }
- }
复制代码 运行结果:
Sun Sep 18 00:00:00 CST 2011
Sun Sep 18 00:00:00 CST 2011
|