尝试调用方法时出错

时间:2017-12-07 07:50:29

标签: java inheritance

我一直在网上寻找,我仍然不确定如何在我的孩子班级中调用方法。我试图在Executive中调用pay()方法,当我在if语句中输入以下代码时,我不断收到错误。

staff[3].awardBonus(bonus);

我一直在使用此方法出错。我不知道如何调用该方法...感谢您的帮助!`import java.util.Scanner;

    import java.util.Scanner;
public class Tester
{
    public static void main (String args[])
    {        
        Scanner scan = new Scanner(System.in);
        StaffMember[] staff = new StaffMember[4];
        String internName = "Susan 2";
        String empName = "Tyler O.";
        String hrName = "Becky R.";
        String execName = "Daniel H.";
        String address = "Brighton";
        String phone = "420 - 0000";
        String SSN = "12345789";
        double rate = 1000;

        staff [0] = new Intern(internName, address, phone);
        staff [1] = new Employee(empName, address, phone, SSN, rate);
        staff [2] = new HourlyEmployee(hrName, address, phone, SSN, rate);
        staff [3] = new Executive(execName, address, phone, SSN, rate);


        for (StaffMember staffPrint : staff)
        {
            System.out.println (staffPrint.toString() + "\n");    
        }

        System.out.println("If you would like to give an executive a bonus, press 1. \nIf you would like to increase the hours of an hourly employee, press 2.");
        int input = scan.nextInt();
        if(input == 1)
        {
            double bonus = 0;
            System.out.println("Enter the bonus for your employee: ");
            bonus = scan.nextDouble(); 

        }
 }

这是Executive类,Employee类和StaffMember类

public class Executive extends Employee
{
    public Executive(String name, String address, String phone, String SSN, double rate)
    {
        super(name, address, phone, SSN, rate);
    }

    public double pay()
    {
        double money = super.pay();
        return money;
    }

    public String toString()
    {
        String employee = super.toString();
        return employee;
    }

    public void awardBonus(double execBonus)
    {
        rate += execBonus;
    }
}

员工

    public class Employee extends StaffMember
{
    String SSN;
    double rate;

    public Employee(String name, String address, String phone, String SSN, double rate)
    {
        super(name, address, phone);
        this.SSN = SSN;
        this.rate = rate;  
    }

    public double pay()
    {
        return rate;
    }

    public String toString()
    {
        String employee = "";        
        employee = ("Name: " + name + "\nAddress: " + address + "\nPhone Number: " + phone + "\nSocial Security Number: " + SSN + "\nPay: " + pay());        
        return employee;
    }
}

StaffMember

public abstract class StaffMember
{
    String name;
    String address;
    String phone;

    public StaffMember(String name, String address, String phone)
    {
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    public String toString()
    {
        String employee = "";        
        employee = ("Name: " + name + "\nAddress: " + address + "\nPhone Number: " + phone);        
        return employee;
    }

    public abstract double pay();
}

4 个答案:

答案 0 :(得分:1)

staff是一个StaffMember数组。当您引用其中的任何项目时(正如您staff[3]那样),您将获得StaffMember

StaffMember没有方法awardBonus()

答案 1 :(得分:1)

您的问题是您尝试在StaffMember对象上调用未定义的方法。实际上,awardBonus()类中没有定义StaffMember方法。

在代码staff[3].awardBonus(bonus)中,您试图在awardBonus() staff[3]实例上调用StaffMember

答案 2 :(得分:1)

通过为所有StaffMembers提供awardBonus,您可以在StaffMember上调用它。例如:

public abstract class StaffMember
{
    public void awardBonus(double bonus) {
        if (bonus > 0) {
            throw new IllegalStateException("Only executives receive a bonus");
        }
    }
...

public class Executive extends StaffMember
{
    @Override
    public void awardBonus(double bonus) {

P.S。小心告知公司。

答案 3 :(得分:0)

您可以使用:

// Check if staff[3] is really an Executive
if(staff[3] instanceof Executive) {
    // Cast staff[3] to an Executive
    Executive executive = ((Executive)staff[3]);
    // Now you can call awardBonus
    executive.awardBonus(bonus);
}

因为staff是一个StaffMember数组,它不包含方法awardBonus
您知道staff[3]Executive,但您的计划并不是staff[3]。因此,您必须检查Executive是否为Executive的实例,以便您可以使用Executive executive = (Executive)staff[3]将其安全地转换为savepoint