【问题标题】:Invoking method in a subclass在子类中调用方法
【发布时间】:2015-01-20 17:19:28
【问题描述】:

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

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

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

我被要求“可以修改 Employee 类以实现接口 Payable 并声明方法 getPaymentAmount 以调用方法 Earnings。然后方法 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

【问题讨论】:

  • 帮您什么,具体

标签: java inheritance subclass


【解决方案1】:

也许这就是您要找的东西?为了实现多态行为,让你的各种 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 会为您解决这个问题。这就是多态性的基础。根据对象的类,它们会调用各自的方法。

【讨论】:

  • 您好,感谢您的帮助。我在上面看到的唯一问题是它涉及编辑子类(例如 SalariedEmployee),这是我们被告知不要做的!因为每个子类都已经实现了收益()方法。例如@Override public double income() { return getWeeklySalary(); } // 结束方法收益
  • @user3600952 除非您发布您拥有的所有内容,否则我只能提供帮助。我不知道income() 最初位于何处/它应该位于何处。
  • 嘿,感谢您的帮助,我在上面的原始帖子中添加了更多代码。
【解决方案2】:

我认为您正在寻找的是这样的:

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;
    } 
}

由于您声明了一个名为earningsabstract 方法,因此abstract class 的其他方法可以调用该方法,因为它们知道Employee 的任何实例化实例都必须具有实现的earnings 方法。

【讨论】:

  • 这正是我想要的!干杯丹尼尔!
【解决方案3】:
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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2011-02-11
    相关资源
    最近更新 更多