java 为什么会出现“找不到合适的构造函数”的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17620320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is there an error "no suitable constructor found"?
提问by Grafica
The error is that there is no "suitable constructor found" for TestPayroll with this statement: Payroll payroll = new Payroll(name, weeksPay); What should the constructor be, and I assume it should be in the Payroll class.
错误是 TestPayroll 没有“找到合适的构造函数”,声明如下: Payroll payroll = new Payroll(name,weekPay); 构造函数应该是什么,我假设它应该在 Payroll 类中。
I want to display the pay for the week for Tiny Tim, Brad Pitt, and Madonna. Thank you!
我想显示小蒂姆、布拉德皮特和麦当娜这周的工资。谢谢!
import javax.swing.JOptionPane;
public class TestPayroll {
private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
private double [] payRate = {100.25, 150.50, 124.25};
private double [] hrsWorked = {40, 35, 36};
private double weeksPay;
//Payroll object
Payroll payroll = new Payroll(name, weeksPay);
public static void main(String[] args) {
//Display weekly pay
JOptionPane.showMessageDialog(null, "%s's pay for the week is: $%.2f\n", payroll[0].getName(), payroll[0].getWeeksPay());
}
}
public class Payroll {
public static void main(String[] args) {
}
private String name;
private double payRate;
private double hrsWorked;
private double weeksPay;
//default constructor
public Payroll() {
this.name = name;
this.payRate = payRate;
this.hrsWorked = hrsWorked;
this.weeksPay = weeksPay;
}
//Payroll constructor
public Payroll(String name, double weeksPay) {
this.name = name;
this.weeksPay = weeksPay;
}
//return name
public String getName() {
return name;
}
//set name
public void setName(String name) {
this.name = name;
}
//return pay rate
public double getPayRate() {
return payRate;
}
//set pay rate
public void setPayRate(double payRate) {
this.payRate = payRate;
}
//return hours worked for the week
public double getHrsWorked() {
return hrsWorked;
}
//set hours worked for the week
public void setHrsWorked(double hrsWorked) {
this.hrsWorked = hrsWorked;
}
//find week's pay
public double getWeeksPay(double weeksPay) {
double weeksPay = payRate * hrsWorked;
return weeksPay;
}
}
回答by Jigar Joshi
You have following constructors for Payroll
您有以下构造函数 Payroll
public Payroll(String name, double weeksPay) {/* some code */}
and
和
public Payroll() {/* some */}
and you are passing String[]
as first argument
你String[]
作为第一个参数传递
回答by Ruchira Gayan Ranaweera
//Payroll constructor. your argument types is String and double.
public Payroll(String name, double weeksPay) {
this.name = name;
this.weeksPay = weeksPay;
}
But
但
//Payroll object
Payroll payroll = new Payroll(name, weeksPay); // you are using String[] array and double as in put arguments.