import java.util.*;public class EmployeeTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];

staff[0] = new Employee("Tom",10000,1988,1,1);
staff[1] = new Employee("Bob",20000,1788,2,2);
staff[2] = new Employee("Jhom",30000,1968,3,3);

for(Employee e:staff)//想把这块改成for(int i=0;i<staff.length;i++的形式该怎么写)
e.raiseSalary(5);

for(Employee e:staff)//还有这块同上
System.out.println("name=" + e.getName()
 + "salary=" + e.getSalary()
 + "hireDay=" + e.getHireDay());
}
}class Employee
{
public Employee(String n, double s, int year,int month, int day)
{
name = n;
salary = s;
GregorianCalendar gc = new GregorianCalendar(year,month-1,day);
hireDay = gc.getTime();
}

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;
}

private String name;
private double salary;
private Date hireDay;
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【cismylife】截止到2008-07-16 16:50:41的历史汇总数据(不包括此帖):
    发帖的总数量:3                        发帖的总分数:80                       每贴平均分数:26                       
    回帖的总数量:2                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:3                        结贴的总分数:80                       
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    敬礼!
      

  2.   


      for (int i = 0; i < staff.length; i++) {
                staff[i].raiseSalary(5);
            }
            for (int i = 0; i < staff.length; i++) {
                System.out.println("name=" + staff[i].getName() + " salary=" + staff[i].getSalary() + " hireDay=" + staff[i].getHireDay());
            }
      

  3.   

    staff数组怎么能直接调用Employee中的方法?
      

  4.   

    跟e没有关系,e只不过是个临时变量而已,这里用staff[i]直接代替了e的作用,相当于:
      for (int i = 0; i < staff.length; i++) {
                Employee e=staff[i];
                e.raiseSalary(5);
      }
      for (int i = 0; i < staff.length; i++) {
                Employee e=staff[i];
                System.out.println("name=" + e.getName() + " salary=" + e.getSalary() + " hireDay=" + e.getHireDay());
      }
      

  5.   


    呵呵,那个不是staff数组在调,表达式是:staff[i],这个表示的是一个Employee对象哈,你那个数组不是就装的是Employee吗?呵呵。
      

  6.   

    继续提问,那这句话是什么意思Employee[] staff = new Employee[3];是为类定义一个数组吗?
      

  7.   

    该行代码表示定义了一个类型为Employee的数组,数组长度为3;也就是说该数组里的元素必须是Employee类型对象