是什么导致我在尝试创建对象时遇到的错误?

时间:2019-07-22 20:39:49

标签: java class main

我正在尝试为创建工资单的类编写程序。我已经看过在线工作,正在尝试创建自己的版本,但是由于围绕main()的内容不断出现错误,因此无法正常运行。

我尝试重新组织代码,创建新文件并重命名,但是仍然出现以下异常:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type Employee is accessible. Must qualify the allocation with an enclosing instance of type Employee (e.g. x.new A() where x is an instance of Employee).
at Employee.main(Employee.java:8)

我希望能够在代码中输入这些值:

import java.util.Scanner;
import java.util.*;

public class Employee{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        PayStub data = new PayStub();

        System.out.println("Enter name: ");
        data.setName(input.next());

        System.out.println("Enter address: ");
        data.setAddress(input.next());

        System.out.println("Enter SSN: ");
        data.setSSN((input.next()));

        System.out.println("Number of hours: ");
        data.setHoursWorked(Double.parseDouble(input.next()));

        System.out.println("Enter hourly rate: ");
        data.setHourlyPayRate(Double.parseDouble(input.next()));

        input.close();
    }
}

public class PayStub {
    private String name;
    private double hoursWorked;
    private double hourlyRate;
    private double fedTaxRate;
    private double stateTaxRate;
    private String address;
    private String phone;
    private String SSN;

    public PayStub() {

    }
    public PayStub(String n, double w, double p) {
        this.name = n;
        this.hoursWorked = w;
        this.hourlyRate = p;
    }

    public double grossPay(double hr, double hw) {
        double grossPay = (hourlyRate * hoursWorked);
        return grossPay;
    }

    public void setFedTax(double fedTaxRate) {
        this.fedTaxRate = fedTaxRate;
    }
    public double getFedTax() {
        return fedTaxRate;
    }

    public void setStateTax(double stateTaxRate) {
        this.stateTaxRate = stateTaxRate;
    }

    public double getStateTax() {
        return stateTaxRate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getHoursWorked() {
        return hoursWorked;
    }

    public void setHoursWorked(double hoursWorked) {
        this.hoursWorked = hoursWorked;
    }

    public double getHourlyPayRate() {
        return hourlyRate;
    }

    public void setHourlyPayRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public String getAddress(){
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone(){
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSSN() {
        return SSN;
    }

    public void setSSN(String SSN) {
        this.SSN = SSN;
    }
}

1 个答案:

答案 0 :(得分:0)

最简单的解决方法是将公共类PayStub移到名为PayStub.java的文件中。

您可以将PayStub声明为静态类,使其成为Employee的嵌套类,但是由于PayStub不是雇员类型,因此没有理由这样做,但这是员工拥有的东西。这是承包商可能拥有的东西,等等...所以您想将其拆分成自己的文件,因为它是可重用的类。