第1个回答 2014-10-14
class Employee {
// 员工数据属性
private String name;
private float salary;
// 带参数的构造方法
public Employee(String name, float salary) {
this.name = name;
this.salary = salary;
}
// 成员变量的访问器和设置器
public String getName() {
return name;
}
public float getSalary() {
return salary;
}
void setName(String name) {
this.name = name;
}
void setSalary(float salary) {
this.salary = salary;
}
// 提供输出员工信息的方法
public void printInfo() {
System.out.println("employee's name: " + name + "\n" + "employee's salary: " + salary);
}
}
// 测试类
public class TestEmployee {
public static void main(String args[]) {
//使用数组,构建多个员工实例
Employee[] employee= new Employee[4];
employee[0] = new Employee("张三", 2000);
employee[1] = new Employee("李四", 3000);
employee[2] = new Employee("王五", 4000);
employee[3] = nwe Employee("胡六", 5000);
// 调用员工方法,输出员工信息
for(int i = 0; i < 4; i++) {
employee[i].printInfo();
}
}
}
// 这台计算机上没有安装Java平台,自行测试下。如果有输入小错误,可以修改