2017-10-16 117 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 

应该这样的。

相关问题