java - 如何将日期信息传递给 dateHired 的 MyDate 类?
问题描述
我试图让 Employee 类发送,day
到MyDate 类。MyDate 类接收 3 个变量并生成一个 toString() 方法供 Employee 类使用。到目前为止我的代码:month
year
员工类
package lab3;
public class Employee extends Person {
String office;
double salary;
int day;
int month;
int year;
MyDate dateHired;
public Employee(String name) {
super(name);
this.office = "No office";
this.salary = 1200.00;
this.day = 1;
this.month = 1;
this.year = 1970;
}
public Employee(String name, String address, String phoneNumber, String email, String office, double salary, int day, int month, int year) {
super(name, address, phoneNumber, email);
this.office = office;
this.salary = salary;
this.day = day;
this.month = month;
this.year = year;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String toString() {
return super.toString() + "\n" + "Office: " + getOffice()
+ "\n" + "Salary: " + getSalary() + "\n" + "Date Hired: " + dateHired.toString();
}
}
MyDate 类
package lab3;
public class MyDate {
public int day, month, year;
public MyDate() {
day = 1;
month = 1;
year = 1970;
}
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String toString() {
return getDay() + "/" + getMonth() + "/" + getYear();
}
}
解决方案
由于您MyDate
在类中有一个属性,因此Employee
您不需要day/month/year
,而是MyDate
在Employee
构造函数中实例化 a
public Employee(String name) {
super(name);
this.office = "No office";
this.salary = 1200.00;
this.dateHired = new MyDate(); // uses the MyDate default constructor
// this.dateHired = new MyDate(1, 1, 1970); // does same with the other constructor
}
public Employee(String name, String address, String phoneNumber, String email,
String office, double salary, int day, int month, int year) {
super(name, address, phoneNumber, email);
this.office = office;
this.salary = salary;
this.dateHired = new MyDate(day, month, year);
}
推荐阅读
- android - 如何在 WebView 中更新请求标头
- angular - 从 Angular HttpClient 使用 Spring Boot StreamingResponseBody
- sql - 我在寻找交叉连接吗?
- elixir - 获取迭代完成长生不老药的计数
- centos - Apache2 - htop 中奇怪的 sh 进程?
- javascript - 如何访问此“对象”的内容?
- mpandroidchart - mpandroidchart 散点图中点的颜色没有变化
- uml - 具有相互依赖的枚举的 UML 图
- python - 谁能告诉我出了什么问题,当我运行它时,浏览器显示“无法访问此站点”
- javascript - 如何仅禁用 html5 datepicker 上的选定日期?