【问题标题】:Compilation error: constructor in class cannot be applied to given types编译错误:类中的构造函数不能应用于给定类型
【发布时间】:2021-02-06 15:19:16
【问题描述】:

我尝试使用超类中的枚举创建子类对象,但是当我尝试在子类中创建对象时出现此错误。

error: constructor Payroll in class Payroll cannot be applied to given types;
        public PayClaim(int hours, PayLevel level){
                                                  ^
  required: PayLevel
  found:    no arguments
  reason: actual and formal argument lists differ in length
1 error

这是我的超类工资单

public class Payroll{
    
    
    static double OVERTIME_RATE = 1.5;

    static int REGULAR_WEEK = 40;
    static int LEVEL_ONE_PAY = 15;
    static int LEVEL_TWO_PAY = 25;
    static int LEVEL_THREE_PAY = 40;
    
    public enum PayLevel{
        ONE, TWO, THREE
    }
    
    private PayLevel levels;
    public Payroll(PayLevel levels){
        this.levels = levels;
    }
    
    public PayLevel getPayLevel(){
        return levels;
    }
    
    public static void main (String [] args) {
        Payroll.OVERTIME_RATE = 1.75;
        Payroll.REGULAR_WEEK = 40;
        Payroll.LEVEL_ONE_PAY = 12;
        System.out.println(Payroll.calculatePay(35, Payroll.PayLevel.ONE));
    }
    
    public static double calculatePay(int noHoursWorked, PayLevel level){
    //do stuff
    }
    
}

这是我的子类 PayClaim

public class PayClaim extends Payroll{
    
    
    int noHoursWorked;
    public Payroll.PayLevel payLevel;
    double calculatedPay = 0;
    
    public static void main (String [] args) {
        PayClaim p = new PayClaim(1, Payroll.PayLevel.ONE);
        System.out.println(p);
    }
    
    public PayClaim(int hours, PayLevel level){
        
        if(hours > 80 || hours < 1){
            throw new IllegalArgumentException();
        }
        else{
            noHoursWorked = hours;
            payLevel = level;
        }
    }
    
    public int getNoHoursWorked(){
        return noHoursWorked;
    }
    
    public PayLevel getPayLevel(){
        return payLevel;
    }
    
    public double getClaculatedPay(){
        return calculatedPay;
    }
    
    public void setCalculatedPay(double pay){
        //
    }
    
    public String toString(){
        //

}

如果我错过了一些微不足道的事情,我深表歉意,因为代码甚至无法编译,所以我真的很难在这里找到我哪里出错了。

【问题讨论】:

  • 我能够自己解决这个问题,首先删除类定义中的 extends Payroll。那么唯一的区别是当你有 Class PayClaim 构造函数而不是编写 PayLevel 级别时,你必须编写 Payroll.PayLevel 级别。然后它就像一个魅力。

标签: java enums compiler-errors extends superclass


【解决方案1】:

我相信您正在寻找的答案非常简单。如果您调用子类的父构造函数,这应该可以解决编译问题。您可以通过使用以下更改来做到这一点。我做的改变是在构造函数的开头,它只是调用父构造函数来创建一个对象,因为它是一个子类。

public class PayClaim extends Payroll{
    
    
    int noHoursWorked;
    public Payroll.PayLevel payLevel;
    double calculatedPay = 0;
    
    public static void main (String [] args) {
        PayClaim p = new PayClaim(1, Payroll.PayLevel.ONE);
        System.out.println(p);
    }
    
    public PayClaim(int hours, PayLevel level){
    enter code here

        super(level);
        
        if(hours > 80 || hours < 1){
            throw new IllegalArgumentException();
        }
        else{
            noHoursWorked = hours;
            payLevel = level;
        }
    }
    
    public int getNoHoursWorked(){
        return noHoursWorked;
    }
    
    public PayLevel getPayLevel(){
        return payLevel;
    }
    
    public double getClaculatedPay(){
        return calculatedPay;
    }
    
    public void setCalculatedPay(double pay){
        //
    }
    
    public String toString(){
        //

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多