java定义一个员工类employee
时间: 2023-06-05 19:48:08 浏览: 362
java代码-定义一个员工信息类Employee。
5星 · 资源好评率100%
Java中定义一个员工类Employee可以包含以下属性和方法:
属性:
- 姓名(name)
- 年龄(age)
- 性别(gender)
- 工号(id)
- 薪水(salary)
方法:
- 构造方法:可以传入姓名、年龄、性别、工号和薪水,初始化对象
- getter和setter方法:用于获取和设置对象的属性值
- toString方法:返回对象的字符串表示,可以输出对象的属性值
示例代码如下:
```
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 +
'}';
}
}
```
阅读全文