Java 在子类中调用方法
我有一个超类(Employee),它实现了一个只包含1个方法的接口,如下所示Java 在子类中调用方法,java,inheritance,subclass,Java,Inheritance,Subclass,我有一个超类(Employee),它实现了一个只包含1个方法的接口,如下所示 public interface Payable { double getPaymentAmount(); // calculate payment; no implementation } 我有许多子类继承自Employee(例如SalariedEmployee、HourlyEmployee、CommissionEmployee),每个子类都包含一个方法 有人叫我去“可以修改类Employee以实现
public interface Payable
{
double getPaymentAmount(); // calculate payment; no implementation
}
我有许多子类继承自Employee(例如SalariedEmployee、HourlyEmployee、CommissionEmployee),每个子类都包含一个方法
有人叫我去“可以修改类Employee以实现接口Payment,并声明方法getPaymentAmount以调用方法earnings。”。方法getPaymentAmount然后将由Employee层次结构中的子类继承。当为特定子类对象调用getPaymentAmount时,它会以多态方式为该子类调用适当的收益方法”
在不编辑子类的情况下,如何在Employee类方法getPaymentAmount中调用收入的相关方法
我是Java的新手
员工类别的相关部分如下所示:
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
以子类的1个示例为例:
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
也许这就是你想要的?为了实现多态行为,让你的不同员工类对
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;
}
}
“如何在员工类方法getPaymentAmount中调用收入的相关方法?”
无需担心。Java将为您解决这一问题。这是多态性的基础。根据对象的类,它们将调用相应的方法。我认为您需要的是如下内容:
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();
}
}
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;
}
}
既然您已经声明了一个名为
earnings
的abstract
方法,那么abstract类的其他方法可以调用该方法,因为它们知道Employee
的任何实例化实例都必须有一个实现的earnings
方法。具体帮助您什么?嘿,谢谢您的帮助。我看到上面的唯一问题是它涉及编辑子类(例如SalariedEmployee),这是我们被告知不要做的!因为每个子类都已经实现了earnings()方法。例如@Override public double earnings(){return getWeeklySalary();}//结束方法earnings@user3600952除非你发布你所有的东西,否则我只能帮你。我不知道收益()最初位于何处/应该位于何处。嘿,感谢你的帮助,我在上面的原始帖子中添加了更多代码。这正是我想要的!丹尼尔,干杯!