Java使用超类,但我没有得到我期望的输出

时间:2017-10-16 15:36:54

标签: java inheritance

您好我已编写此程序实现具有以下字段和方法的超类Employee。

字段:

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

问题是我希望看到:

  

员工姓名:Charles Dickens员工ID: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();
    }
}

1 个答案:

答案 0 :(得分:0)

好的,我相信问题在于你将整件事放在一个文件中。

尝试将TestEmployee类移动到自己的文件,并将类重命名为

public class TestEmployee

它应该那样工作。