现在要求编写employee类的tostring方法,返回的字符串格式为:父类的tostring-company的tostring-salary
时间: 2023-04-19 20:00:50 浏览: 139
好的,可以这样编写employee类的tostring方法:
```
public String toString() {
return super.toString() + "-" + company.toString() + "-" + salary;
}
```
其中,super.toString()表示调用父类的tostring方法,company.toString()表示调用company对象的tostring方法,salary表示该员工的薪水。最终返回的字符串格式为:父类的tostring-company的tostring-salary。
相关问题
有Person类,Company类,Employee类。 其中Employee类继承自Person类,属性为: private Company company; private double salary; 现在要求编写Employee类的toString方法,返回的字符串格式为:父类的toString-company的toString-salary 函数接口定义:
```java
public class Employee extends Person {
private Company company;
private double salary;
// 构造函数
public Employee(String name, int age, Company company, double salary) {
super(name, age);
this.company = company;
this.salary = salary;
}
// toString方法
@Override
public String toString() {
return super.toString() + "-" + company.toString() + "-" + salary;
}
}
```
JAVA * 定义私有属性: * 字符串名称;* 整数年龄;* 字符串性别;* 国际工资;* Date hiredate;//入职时间 * * 定义构造方法,以及属性get,set方法.* 定义toString方法,格式如: * 姓名:张三,年龄:25,性别:男,薪资:5000,入职时间:2006-02-15 * * 定义等于方法,要求只要相同,则认为名字内容一致。并测试
以下是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.toString();
}
@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);
}
public static void main(String[] args) {
// 创建两个Employee对象
Employee employee1 = new Employee("张三", 25, "男", 5000, new Date(2006, 1, 15));
Employee employee2 = new Employee("张三", 30, "男", 6000, new Date(2010, 5, 20));
// 测试toString方法
System.out.println(employee1.toString());
System.out.println(employee2.toString());
// 测试equals方法
System.out.println("employee1和employee2是否内容相同:" + employee1.equals(employee2));
}
}
```
上述代码中定义了`Employee`类,包含了私有属性`name`、`age`、`gender`、`salary`和`hiredate`。构造方法用于初始化对象,属性的get和set方法用于获取和设置属性的值。`toString`方法用于返回对象的字符串表示,格式为所需的输出格式。`equals`方法用于判断两个对象是否内容相同,只要名字相同即认为内容一致。在`main`方法中,我们创建了两个Employee对象,并测试了`toString`方法和`equals`方法。
阅读全文