-3

根据下面的代码,我需要通过创建一个名为employee print() 的专用打印方法来修改Employee 类中的代码,并展示如何在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);
}

}

4

3 回答 3

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();
于 2013-03-24T11:01:23.170 回答
0
class Employee1
{
  String empName;
  String empNum;
  String empEmailAddress;
  int yearOfBirth;
  @Override toString(){
    System.out.println(empName + empNum + empEmailAddress + yearOfBirth);
  }
}

emp.toString();
于 2013-03-24T11:03:54.060 回答
0

您可以像这样覆盖 Employee 类中的 toString() 方法

class Employee1{
  String empName;
  String empNum;
  String empEmailAddress;
  int yearOfBirth;
@Override
public String toString(){
return empName + emNum +.....
}
}

并使用它在 EmployeeTest 中打印sysout(instanceOfEmployee)

于 2013-03-24T11:17:22.783 回答