2013-03-24 86 views
-3

基於下面的代碼,我需要通過創建一個名爲employee print()的專用打印方法來修改Employee類中的代碼,並顯示它將如何用於打印員工測試班!請幫忙嗎?下面是代碼:如何創建和使用使用java的打印方法

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 = "[email protected]"; 



    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); 
} 

}

+1

你真的應該告訴我們你第一次嘗試的東西。 – rainkinz 2013-03-27 23:24:52

+0

是的,你是對的兄弟,但是這條路很漫長,我想出了這段代碼!它沒有給我這樣的代碼,我經歷了所有的步驟,但我沒能實現最後一步方法!謝謝你的評論兄弟! – user2204281 2013-04-08 16:40:24

回答

1

你一個名爲印刷方法添加到稱爲打印

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 
     } 
} 

Employee類,並調用它像這樣:

// these lines replace the System.out.println block in your code 
emp1.print(); 
emp2.print(); 
+0

Thnx我很appriciate! – user2204281 2013-03-27 08:07:48

0
class Employee1 
{ 
    String empName; 
    String empNum; 
    String empEmailAddress; 
    int yearOfBirth; 
    @Override toString(){ 
    System.out.println(empName + empNum + empEmailAddress + yearOfBirth); 
    } 
} 

emp.toString(); 
0

您可以在類中重寫toString()方法這樣

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

,並使用該員工打印EmployeeTest sysout(instanceOfEmployee)

+0

Thnx很多我appriciate! – user2204281 2013-03-27 08:08:45