简体   繁体   English

抽象方法和构造函数

[英]Abstract Methods and Constructors

First off, this isn't an assignment question. 首先,这不是作业问题。 It's a revision question from past exam papers. 这是过去试卷的修订问题。

Ok so the question is: 好的,所以问题是:

Implement an abstract base class called Employee that is used to hold and access basic information about an employee eg name, address, etc. This class should also define an abstract method called earnings() that returns the weekly pay for each type of employee. 实现一个称为Employee的抽象基类,该基类用于保存和访问有关员工的基本信息,例如姓名,地址等。该类还应定义一个名为Income()的抽象方法,该方法返回每种类型的员工的每周薪水。 The class should include a suitable constructor and accessor methods to retrieve information about the employee. 该类应包括合适的构造函数和访问器方法,以检索有关员工的信息。

The code I have so far is: 到目前为止,我的代码是:

abstract public class Employee {

    public String Full_Name;
    public int IDNum;
    public String FullAddress;
    public int hours_worked;
    public int hour_pay;

    Employee(String name, int ID, String Address){
        Full_Name = name;
        IDNum = ID;
        FullAddress = Address;
    }

    abstract public int earnings(){
        return int week_Sal = hours_worked*hour_pay;
    }

}

So my question is, how would I pass the different parameters for each employee into the earnings method, so I can then calculate their earnings? 所以我的问题是,如何将每个员工的不同参数传递到收入方法中,然后才能计算他们的收入?

In java, an abstract method declaration doesn't include a method body. 在Java中,抽象方法声明不包含方法主体。 You just declare that the method should exist; 您只需要声明该方法应该存在即可。 non-abstract subclasses of the class have to provide a version of the method. 该类的非抽象子类必须提供该方法的版本。

This is an example of what the question is looking for: 这是问题正在寻找的示例:

abstract public class Employee {

    public String Full_Name;
    public int IDNum;
    public String FullAddress;

    Employee(String name, int ID, String Address){
        Full_Name = name;
        IDNum = ID;
        FullAddress = Address;
    }

    abstract public int earnings();
}

public class HourlyEmployee extends Employee {
    private int hours_worked;
    private int hour_pay;

    HourlyEmployee(String name, int ID, String Address, int hour_pay) {
        super(name, ID, Address);
        this.hour_pay = hour_pay;
    }
    public void setHoursWorked(int hours_worked) {
        this.hours_worked = hours_worked;
    }
    @Override
    public int earnings() {
        return hours_worked * hour_pay;
    }
}

public class CEO extends Employee {
    private long year_pay;
    CEO(String name, int ID, String Address, long year_pay) {
        super(name, ID, Address);
        this.year_pay = year_pay;
    }
    @Override
    public int earnings() {
        // Convert yearly pay to weekly pay
        return (int) (year_pay / 52);
    }
}

The base class just declares that there's a method which returns the weekly earnings. 基类只是声明有一个返回每周收入的方法。 Subclasses provide their own version of the method, and each subclass could implement the method in a completely different way. 子类提供了自己的方法版本,每个子类可以以完全不同的方式实现该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM