Compilation error: constructor in class cannot be applied to given types
Compilation error: constructor in class cannot be applied to given types
我正在尝试使用来自超级 class 的枚举创建子class 对象,但是当我尝试在子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
这是我的超级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
}
}
这是我的子class 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(){
//
}
我正在尝试使用来自超级 class 的枚举创建子class 对象,但是当我尝试在子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
这是我的超级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
}
}
这是我的子class 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(){
//
}