写一段Java代码中定义一个员工类Employee要求包含以下属性和方法: 属性: 姓名(name) 年龄(age) 性别(gender) 工号(id) 薪水(salary) 方法: 构造方法:可以传入姓名、年龄、性别、工号和薪水,初始化对象 getter和setter方法:用于获取和设置对象的属性值 toString方法:返回对象的字符串表示,可以输出对象的属性值
时间: 2023-04-02 09:00:40 浏览: 253
好的,以下是Java代码:
public class Employee {
private String name;
private int age;
private String gender;
private String id;
private double salary;
public Employee(String name, int age, String gender, String id, double salary) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", id='" + id + '\'' +
", salary=" + salary +
'}';
}
}
阅读全文