Java 编译错误:类中的构造函数无法应用于给定类型
我正试图使用超类中的枚举创建一个子类对象,但是当我试图在子类中创建对象时,我得到了这个错误Java 编译错误:类中的构造函数无法应用于给定类型,java,enums,compiler-errors,extends,superclass,Java,Enums,Compiler Errors,Extends,Superclass,我正试图使用超类中的枚举创建一个子类对象,但是当我试图在子类中创建对象时,我得到了这个错误 error: constructor Payroll in class Payroll cannot be applied to given types; public PayClaim(int hours, PayLevel level){ ^ required: PayLevel
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(){
//
}
公共类工资索赔扩展工资单{
工作一小时;
公共工资单。工资水平工资水平;
双重计算工资=0;
公共静态void main(字符串[]args){
PayClaim p=新的PayClaim(1,Payroll.PayLevel.1);
系统输出println(p);
}
公共薪资申请(整数小时,薪资级别){
如果(小时>80 | |小时<1){
抛出新的IllegalArgumentException();
}
否则{
工作时间=小时;
工资水平=工资水平;
}
}
public int getNoHoursWorked(){
返回工作小时数;
}
公共PayLevel getPayLevel(){
报酬水平;
}
公共双倍getClaculatedPay(){
返回计算工资;
}
公共无效设置计算工资(双倍工资){
//
}
公共字符串toString(){
//
}
如果我错过了一些琐碎的东西,我很抱歉,因为代码甚至无法编译,所以我真的很难找到哪里出了问题。我相信你要寻找的答案非常简单。如果你调用子类的父构造函数,这应该可以解决编译问题。你可以使用foll我所做的更改是在构造函数的开头,它只是调用父构造函数来创建一个对象,因为它是一个子类
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(){
//
}
公共类工资索赔扩展工资单{
工作一小时;
公共工资单。工资水平工资水平;
双重计算工资=0;
公共静态void main(字符串[]args){
PayClaim p=新的PayClaim(1,Payroll.PayLevel.1);
系统输出println(p);
}
公共薪资申请(整数小时,薪资级别){
在这里输入代码
超级(级别);
如果(小时>80 | |小时<1){
抛出新的IllegalArgumentException();
}
否则{
工作时间=小时;
工资水平=工资水平;
}
}
public int getNoHoursWorked(){
返回工作小时数;
}
公共PayLevel getPayLevel(){
报酬水平;
}
公共双倍getClaculatedPay(){
返回计算工资;
}
公共无效设置计算工资(双倍工资){
//
}
公共字符串toString(){
//
}
首先,我可以自己解决这个问题,删除类定义中的Extendes Payroll。然后唯一的区别是,当您使用类PayClaim构造函数而不是编写PayLevel时,您必须编写Payroll.PayLevel。然后它就像一个符咒一样工作。