在子类中调用方法

时间:2015-01-20 17:19:29

标签: java inheritance subclass

我有一个问题,我有一个超类(Employee),它实现了一个只包含1个方法的接口,如下所示。

public interface Payable 
{    
   double getPaymentAmount(); // calculate payment; no implementation
}

我有许多继承自Employee的子类(例如SalariedEmployee,HourlyEmployee,CommissionEmployee),每个子类都包含方法收入。

我被要求"可以修改类Employee来实现Payable接口并声明方法getPaymentAmount来调用方法收益。然后,方法getPaymentAmount将由Employee层次结构中的子类继承。当为特定的子类对象调用getPaymentAmount时,它会以多态方式为该子类调用适当的收益方法"。

有人可以帮我解决这个问题吗?如何在不编辑子类的情况下在Employee类方法getPaymentAmount中调用相关的收益方法?

我是Java的相对新手所以非常感谢

Employee类的相关部分如下:

public abstract class Employee implements Payable
{
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;

   // three-argument constructor
   public Employee( String first, String last, String ssn )
   {
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
   } // end three-argument Employee constructor
    //getters, settters, toString override etc have been deleted. 
   public double getPaymentAmount()
   {
       ????    //This is what I need help with.        
   }

} // end abstract class Employee

并以子类为例:

public class SalariedEmployee extends Employee
{

    private double weeklySalary;

    // four-argument constructor
    public SalariedEmployee(String first, String last, String ssn, double salary)
    {
        super(first, last, ssn); // pass to Employee constructor
        setWeeklySalary(salary); // validate and store salary
    } // end four-argument SalariedEmployee constructor

    @Override
    public double earnings()
    {
        return getWeeklySalary();
    } // end method earnings

} // end class SalariedEmployee

3 个答案:

答案 0 :(得分:0)

也许这就是你要找的?要实现多态行为,请让各种Employee类具有earnings() / getPaymentAmount()的不同实现。这样做会导致方法覆盖其超类,从而实现多态性。

class Employee implements Payable
{
    double getPaymentAmount(){
        return earnings();
    }
    double earnings(){
        //Your other implementations
        return xxx;
    }   
}

class SalariedEmployee extends Employee
{
    double getPaymentAmount(){
        return earnings();
    }
    double earnings(){
        //Different implementation for different employee tpye
        return xxx;
    } 
}

"如何在Employee类方法getPaymentAmount中调用相关的收益方法?"

没有必要担心这一点。 Java会为您解决这个问题。这是多态性的基础。根据对象的类,他们将调用相应的方法。

答案 1 :(得分:0)

我认为你所寻找的是这样的:

abstract class Employee implements Payable
{
    double getPaymentAmount(){
        return earnings();
    }

    abstract double earnings();
}

class SalariedEmployee extends Employee
{

    double earnings(){
        //Different implementation for different employee tpye
        return xxx;
    } 
}

由于您已声明名为abstract的{​​{1}}方法,earnings的其他方法可以调用该方法,因为他们知道任何实例化的abstract class实例必须具有已实施Employee方法。

答案 2 :(得分:-1)

Employee.java
=======================================================================
interface Payable
{
    double getPaymentAmount(); // calculate payment; no implementation
}

public abstract class Employee implements Payable{
    public double getPaymentAmount()
    {
        return 0.0;
    }

    public void printSalary()
    {

    }
}


Teacher.java
=======================================================================
public class Teachers extends Employee {
    public double getPaymentAmount()
    {
        return 5;
    }

    public void printSalary()
    {
        System.out.println("Teachers current salary is: " + getPaymentAmount());
    }

}

SoftwareEngineer.java
=======================================================================
public class SoftwareEngineer extends Employee {
    public double getPaymentAmount()
    {
        return 500;
    }

    public void printSalary()
    {
        System.out.println("Software Engineers current salary is: " + getPaymentAmount());
    }

}

TestEmployee.java
=======================================================================
public class TestEmployee {
    public static void main(String s[])
    {
        Employee e = new Teachers();
        e.printSalary();

        Employee e1 = new SoftwareEngineer();
        e1.printSalary();
    }
}