当我尝试在Java的主类中创建对象时找不到符号

时间:2016-03-11 20:45:17

标签: java

这是我的主要内容。它是一个程序,通过每周工资,基本+工资或每小时+工资支付来计算员工每周工资。所以我试图创建一个对象然后遇到这个障碍。

public class Lab3A {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Employee emp1 = new Employee (bob);
    }

}

这是我的类对象。

public class Employee {
    private static int empCount = 0; 
    private String empName; 
    private int empNumber;
    private double empSalary;
    private double empRate;
    private double empHours;
    private double empBase;
    private int empPieces;
    private int empType;

    public Employee (String name){
        name = this.empName;
        empCount ++;
    }

    public void setEmployeePay (double salary){
        salary= this.empSalary;
    }

    public void setEmployeePay (double rate, double hours){
        rate = this.empRate;
        hours = this.empHours;
    }

    public void setEmployeePay (double base, int pieces){
        base = this.empBase;
        pieces = this.empPieces;
    }

    public static int getCount (){
        return empCount;
    }

    public String getName(){
         return empName;
    }

    public int getNumber (int max, int min){
        return ((int) (Math.random()*(max - min))) + min;
    }
    public double calculatePay(){
        if (empCount == 1)
        {
            if (empHours > 0 && empRate > 0){
                double pay = (empHours - 40) * 1.5;
                return pay;
            } else {
                double pay = empRate * empHours;
                return pay;
            }
        } else if (empSalary > 0){
            double pay = empSalary / 52;
            return pay;
        } else {
            double pay = empBase + empPieces * 24;
            return pay;
        }

    }
}
如果有人能澄清我做错了什么,我们将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

您需要"bob",而不仅仅是bob

"bob"被视为字符串文字。仅bob被解释为变量标识符,并且您没有标记为bob的变量。

您也可以在创建对象之前创建名为String的{​​{1}}变量:

bob

但在这种情况下,最好将变量标记为String bob = "bob"; Employee employee = new Employee(bob); ,因为它代表一个名称。