基于下面的代码,我需要通过创建一个名为employee print()的专用打印方法来修改Employee类中的代码,并展示它将如何用于在员工Test类中打印!有什么帮助吗?这是代码:
class Employee1{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
}
public class employeeTest {
public static void main(String[] args) {
Employee1 emp1 = new Employee1();
emp1.empName = "Sam";
emp1.empNum = "213-23-978";
emp1.empEmailAddress = "sammy@company.za";
Employee1 emp2 = new Employee1();
emp2.empName = "Tasha";
emp2.empNum = "315-90-274";
emp2.yearOfBirth = 1982;
System.out.println("Employee Name: " + emp1.empName);
System.out.println("Employee Number: " + emp1.empNum);
System.out.println("Email Adress: " + emp1.empEmailAddress);
System.out.println("Year of Birth: " + emp1.yearOfBirth);
System.out.println("Employee Name: " + emp2.empName);
System.out.println("Employee Number: " + emp2.empNum);
System.out.println("Email Address: " + emp2.empEmailAddress);
System.out.println("Year of Birth: " + emp2.yearOfBirth);
}
}
答案 0 :(得分:1)
您将名为print的方法添加到名为print
的员工类中class Employee1{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
public void print() {
System.out.println("Employee name:" + empName);
System.out.println("Employee number:" + empNum);
// etc
}
}
并将其称为:
// these lines replace the System.out.println block in your code
emp1.print();
emp2.print();
答案 1 :(得分:0)
class Employee1
{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
@Override toString(){
System.out.println(empName + empNum + empEmailAddress + yearOfBirth);
}
}
emp.toString();
答案 2 :(得分:0)
您可以像这样
覆盖类Employee中的toString()方法class Employee1{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
@Override
public String toString(){
return empName + emNum +.....
}
}
并使用它在EmployeeTest sysout(instanceOfEmployee)