1. import java.util.*;
 2.
 3. public class EmployeeTest
 4. {
 5.    public static void main(String[] args)
 6.    {
 7.       // fill the staff array with three Employee objects
 8.       Employee[] staff = new Employee[3];
 9.
10.       staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
11.       staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
12.       staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
13.
14.       // raise everyone's salary by 5%
15.       for (Employee e : staff)
16.          e.raiseSalary(5);
17.
18.       // print out information about all Employee objects
19.       for (Employee e : staff)
20.          System.out.println("name=" + e.getName()
21.             + ",salary=" + e.getSalary()
22.             + ",hireDay=" + e.getHireDay());
23. }
24. }
25.
26. class Employee
27. {
28.    public Employee(String n, double s, int year, int month, int day)
29.    {
30.       name = n;
31.       salary = s;
32.       GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
33.          // GregorianCalendar uses 0 for January
34.       hireDay = calendar.getTime();
35.    }
36.
37.    public String getName()
38.    {
39.       return name;
40.    }
41.
42.    public double getSalary()
43.    {
44.       return salary;
45.    }
46.
47.    public Date getHireDay()
48.    {
49.       return hireDay;
50.    }
51.
52.    public void raiseSalary(double byPercent)
53.    {
54.       double raise = salary * byPercent / 100;
55.       salary += raise;
56.    }
57.
58.    private String name;
59.    private double salary;
60.    private Date hireDay;
61. }
上面这段程序,在1.5里面就会run 成功,但是,1。6。2 就不行。
问题如下:
incompatible types
麻烦诸位了,上面这段程序从java 2 核心技术 copy 过来的。

解决方案 »

  1.   

    这是我在1.6.0 编译运行的结果
    name= Carl   Cracker ,salary= 78750.0,hireDay= Tue Dec 15 00:00:00 CST 1987
    name= Harry   Hacker ,salary= 52500.0,hireDay= Sun Oct 01 00:00:00 CST 1989
    name= Tony   Tester ,salary= 42000.0,hireDay= Thu Mar 15 00:00:00 CST 1990
    没有报错,希望对你有些帮助。
      

  2.   

    我又重装了下jdk,结果还是一样的,难道是什么地方设置的问题?
      

  3.   

    诸位:我把hireDay得类型都变成Sting了,而且 hireDay   =   calendar.getTime(); 变成hireDay   =   calendar.getTime().toString(); 
    就过去了。大家一起来研究研究。为啥呢
      

  4.   

    这个程序在 JDK 6 中通过不过好像是不可能的。不兼容的类型?没有不兼容啊,calendar.getTime() 返回的就是 Date 类型啊。
      

  5.   

    用try...catch block 处理异常