JAVA * 定义私有属性: * String name; * int age; * String gender; * int salary; * Date hiredate;//入职时间 * * 定义构造方法,以及属性get,set方法. * 定义toString方法,格式如: * 张三,25,男,5000,2006-02-15 * * 定义equals方法,要求名字以及年龄相同,则认为内容一致。
时间: 2023-08-11 12:04:24 浏览: 106
以下是您所需的Java代码:
```java
import java.util.Date;
public class Employee {
private String name;
private int age;
private String gender;
private int salary;
private Date hireDate;
public Employee(String name, int age, String gender, int salary, Date hireDate) {
this.name = name;
this.age = age;
this.gender = gender;
this.salary = salary;
this.hireDate = hireDate;
}
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 int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
@Override
public String toString() {
return name + "," + age + "," + gender + "," + salary + "," + hireDate;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Employee employee = (Employee) obj;
return name.equals(employee.name) && age == employee.age;
}
}
```
以上代码定义了一个名为Employee的类,私有属性包括name、age、gender、salary和hireDate。构造方法用于初始化这些属性,get和set方法用于获取和设置属性的值。toString方法用于返回对象的字符串表示形式,equals方法用于判断两个Employee对象是否相等(根据name和age判断)。
阅读全文