为什么会出现“找不到合适的构造函数”的错误?

时间:2013-07-12 17:07:36

标签: java

错误是TestPayroll没有“找到合适的构造函数”这个语句:Payroll payroll = new Payroll(name,weeksPay); 构造函数应该是什么,我认为它应该在Payroll类中。

我想显示Tiny Tim,Brad Pitt和Madonna这周的薪水。谢谢!

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;
        }   

}

3 个答案:

答案 0 :(得分:6)

您有Payroll

的以下构造函数
    public Payroll(String name, double weeksPay) {/* some code */}

    public Payroll() {/* some */}

您正在传递String[]作为第一个参数

答案 1 :(得分:1)

   //Payroll constructor. your argument types is String and double.
    public Payroll(String name, double weeksPay) {
        this.name = name;
        this.weeksPay = weeksPay;
    }

但是

  //Payroll object
  Payroll payroll = new Payroll(name, weeksPay); // you are using String[] array and double as in put arguments.

答案 2 :(得分:0)

这是您要调用的方法,它将一个参数作为单个字符串:

//Payroll constructor
        public Payroll(String name, double weeksPay)

以下是您正在拨打的电话:

    //Payroll object
    Payroll payroll = new Payroll(name, weeksPay);

在将对象引用为字符串数组对象而不是传递单个字符串的情况下:

    private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};

尝试使用枚举器将值一一传递。 要么 您可以为字符串数组创建一个构造函数,在该数组中,需要将相同的逻辑添加到构造函数而不是main方法中。