几个问题:
1
staff[1] = Employee("Harry Cracker",50000,1989,10,1);
staff[2] = Employee("Tommy Tester",40000,1990,3,15);
改为
staff[1] = new Employee("Harry Cracker",50000,1989,10,1);
staff[2] = new Employee("Tommy Tester",40000,1990,3,15);2
public String getName <---漏掉了括号
{
return name;
}
1
staff[1] = Employee("Harry Cracker",50000,1989,10,1);
staff[2] = Employee("Tommy Tester",40000,1990,3,15);
改为
staff[1] = new Employee("Harry Cracker",50000,1989,10,1);
staff[2] = new Employee("Tommy Tester",40000,1990,3,15);2
public String getName <---漏掉了括号
{
return name;
}
public String getName
{
return name;
}应该改成:
public String getName()
{
return name;
}
我很喜欢你把field definition放在method definition后
现在我改好可以compile 并运行成功
/**
*GregeorianCalendar calendar= new GregeorianCalendar(year,month-1,day);
* //GregeorianCalendar uses 0 for January
* hireDay = calendar.getTime();
*我没找到这类我直接用Date类
*hireDay = new Date(year,month-1,day);
*/
import java.util.*;public class ManagerTest
{
public static void main(String[] args)
{
//construct a manager object
Manager boss = new Manager("Carl Cracker",80000,1978,12,15);
boss.setBonus(50000);
Employee[] staff = new Employee[3]; //fill the staff array with Manager and Employee objects
staff[0] = boss;
staff[1] = new Employee("Harry Cracker",50000,1989,10,1);
staff[2] = new Employee("Tommy Tester",40000,1990,3,15); //print out the information about all Employee
// objects
for( int i=0;i< staff.length;i++)
{
Employee e = staff[i];
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}
}
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee(String n,double s,int year,int month,int day)
{
name = n;
salary = s;
//GregeorianCalendar calendar= new GregeorianCalendar(year,month-1,day);
//GregeorianCalendar uses 0 for January
hireDay = new Date(year,month-1,day);
}
public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public Date getHireDay()
{
return hireDay;
} public void raiseSalary(double byPercent)
{
double raise = salary*byPercent / 100;
salary += raise;
}}class Manager extends Employee
{
private double bonus;
public Manager(String n,double s,int year,int month,int day)
{
super(n,s,year,month,day);
bonus = 0;
}
public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
} public void setBonus(double b)
{
bonus = b;
}
}