2017-10-16 40 views
-4

嗨我已經編寫了這個程序,它實現了具有以下字段和方法的超類Employee。Java與超類一起工作,但我沒有得到我期望的輸出

領域:

String firstName 
String lastName 
int employeeID 
double salary 

方法:

Constructor(): initialize balance field to null and zero. 
Setters and getters for firstName, lastName, and employeeID 
EmployeeSummary() – prints all account attributes 
Part 2: Implement a Manager class that inherits from the Employee class. 

有一個部門屬性 方法:

EmployeeSummary() – prints all superclass and subclass attributes 

的問題是我希望看到:

員工姓名:查爾斯·狄更斯員工編號:34599薪金:6500.0
部:應收

的輸出,但我得到什麼.... 任何幫助是極大的讚賞。 這裏是代碼:

package week1john_huber; 

import java.util.*; 
import java.lang.*; 
import java.io.*; 

class Employee { 
    //attributes of Employee class 
    private String firstName; 
    private String lastName; 
    private int employeeID; 
    private double salary; 


    public Employee() { //default constructor 
     firstName = null; 
     lastName = null; 
     employeeID = 0; 
     salary = 0.0; 

    } 

    public void setFirstName(String fname) { //set and get methods for all attributes 
     firstName = fname; 
    } 

    public String getFirstname() { 
     return firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lname) { 
     lastName = lname; 
    } 

    public double getEmployeeID() { 
     return employeeID; 
    } 

    public void setEmployeeID(int empId) { 
     employeeID = empId; 

    } 

    public double getSalary() { 
     return salary; 
    } 

    public void setSalary(double s) { 
     salary = s; 
    } 

    public void EmployeeSummary() { //display all attributes of Employee 
     System.out.println("Employee Name: " + firstName + " " + lastName + " Employee Id :" + employeeID + " salary: " + salary); 
    } 
} 

class Manager extends Employee { 
    private String department; 


    public Manager() { //default constructor 
     super();       //calling superor base class default constructor 
     department = null; 
    } 

    public String getDepartment() { 
     return department; 
    } 

    public void setDepartment(String dept) { //set and get methods for department 
     department = dept; 
    } 

    public void EmployeeSummary() { 
     super.EmployeeSummary();  //calling super class method with same name 
     System.out.println("Department : " + department); 
    } 
} 

class TestEmployee { 
    public static void main(String[] args) { 
     Manager mgr = new Manager(); 
     mgr.setFirstName("Charles");  //all set methods of super class are available to derived class 
     mgr.setLastName("Dickens"); 
     mgr.setEmployeeID(34599); 
     mgr.setSalary(6500); 
     mgr.setDepartment("Accounts"); 
     mgr.EmployeeSummary(); 
    } 
} 
+0

你確定你正在運行你的代碼嗎?還有類TesyEmployee必須公開 –

+2

我已經運行你的代碼,它返回你應該說的確切數據: 員工姓名:Charles Dickens員工編號:34599工資:6500.0 部門:賬戶 – skw

+0

請檢查您的執行環境(即IDE),因爲它可能是發送stdout到一個地方,你不認爲 –

回答

0

好吧,我相信問題是,你必須在一個文件中,整個事情的事實。

嘗試TestEmployee類移動到它自己的文件和類重命名爲

public class TestEmployee 

應該這樣的。

相關問題