0

我有 3 个类:公司、部门和员工。公司由部门组成,部门由员工组成。很基本。但是当我尝试设置每个员工的部门时,我收到以下错误:

  • 令牌“countingGuru”的语法错误,此令牌后应为 VariableDeclaratorId
  • 标记的语法错误,错误的构造

我已经用谷歌搜索了这些错误,但我仍然无法弄清楚我做错了什么。

这是代码:

public class Company {
    static String[] validDeptNames = {
        "Accounting", "Human Resources", "Information Systems", "Marketing"
    };

    static Department accounting = new Department("Accounting");
    static Department marketing = new Department("Marketing");
    static Department infoSys = new Department("Information Systems");
    static Department humanRes = new Department("Human Resources");

    public static void main(String[] args) {

    }  
}

import java.util.ArrayList;

public class Department {
    Department department;

    ArrayList<Employee> employees;
    Department(String deptName){
        employees = new ArrayList<Employee>();

    }

    static Employee countingGuru = new Employee("Counting Guru", 55);
    static Employee countingPro = new Employee("Counting Pro", 45);
    static Employee countingSavvy = new Employee("Counting Savvy", 40);
    static Employee countingNovice = new Employee("Counting Novice", 25);
    static Employee salesGuru = new Employee("Sales Guru", 50);
    static Employee salesPro = new Employee("Sales Pro", 48);
    static Employee salesSavvy = new Employee("Sales Savvy", 38);
    static Employee hiringGuru = new Employee("Hiring Guru", 58);
    static Employee hiringPro = new Employee("Hiring Pro", 47);
    static Employee hackingPro = new Employee("Hacking Pro", 46);
    static Employee hackingGuru = new Employee("Hacking Guru", 51);
    static Employee hackingSavvy = new Employee("Hacking Savvy", 38);
    static Employee hackingNovice = new Employee("Hacking Novice", 23);

    public void addEmployee(Employee employee){
        employee.setDepartment(this);
        employees.add(employee);
    }

    accounting.addEmployee(countingGuru);

}
public class Employee implements Comparable<Employee> {
    String empName;
    int empAge;
    Department department;

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    String name;
    int age;
    public Employee(String name, int age) {
        this.name = name;
        this.age  = age;
    }

    public String getName() {
        return empName;
    }

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

    public int getAge() {
        return empAge;
    }

    public void setAge(int age) {
        this.empAge = age;
    }

    @Override
    public int compareTo(Employee arg0) {
        return 0;
    }
}
4

2 回答 2

2

你有accounting.addEmployee(countingGuru);任何功能之外。像这样的非变量声明需要在方法、构造函数或静态块中。

由于accounting是 的static成员Company,因此您需要将其引用为Company.accounting.addEmployee(countingGuru);

顺便说一句,拥有这么多东西并不是很好的设计static,因为这意味着Company您创建的每一个对象都将拥有相同的会计、营销、信息系统和人力资源部门。您应该使这些字段非静态并在Company的构造函数中初始化它们。

于 2013-10-24T18:09:25.780 回答
1

从语法上讲,如果将 Department 的构造函数更改为此

Department(String deptName){
    employees = new ArrayList<Employee>();
    Company.accounting.addEmployee(countingGuru);
}

并删除该行

    accounting.addEmployee(countingGuru);

然后,您的代码将没有错误。

于 2013-10-24T18:32:52.710 回答