gpt4 book ai didi

java - 编译错误: constructor in class cannot be applied to given types

转载 作者:行者123 更新时间:2023-12-02 10:52:24 24 4
gpt4 key购买 nike

我试图使用父类(super class)中的枚举创建子类对象,但是当我尝试在子类中创建对象时,出现此错误。

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
这是我的父类(super class)工资单
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(){
//

}
如果我错过了一些琐碎的事情,我很抱歉,只是代码甚至无法编译,所以我真的很难在这里找到我要去的地方。

最佳答案

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

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(){
//

}

关于java - 编译错误: constructor in class cannot be applied to given types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64502029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com