2013-10-24 143 views
0

我有3个类:公司,部门和员工。公司由部门组成,部门由员工组成。很基本。但是,当我尝试设置每个员工的部门,我收到以下错误:java:将对象添加到对象的ArrayList;得到错误

    令牌“countingGuru”
  • 语法错误,VariableDeclaratorId预期后令牌(S),错位构建此令牌
  • 语法错误(S )

我已经使用了错误,但我仍然无法弄清楚我做错了什么。

下面是代码:

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

只是一个普遍的说法,你有领域和getters领域,但他们没有被宣布为私人。除非明确声明,Java字段默认为包访问。我知道一些语言不同,特别是Objective-C是相反的(默认情况下是私有的)。让公共/受保护领域的制定者和获取者当然是多余的。 – Radiodef

+0

你的代码是一团糟。你应该先阅读教程。 –

+0

@Radiodef谢谢你,我会解决这个问题。 – thatpaintingelephant

回答

2

您有任何accounting.addEmployee(countingGuru);函数之外。像这样的非变量声明需要在方法,构造函数或static block中。

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

顺便说一下,这并不是很好的设计,因此它有很多东西都是static,因为这意味着您创建的每个单独的Company对象将具有相同的会计,市场营销,infoSys和humanRes部门。您应该使这些字段非静态并在Company的构造函数中将它们初始化。

+0

啊,我明白了。所以这样的事情? public void addToDepartment(){ \t \t accounting.add(countingGuru); \t} – thatpaintingelephant

+0

因为accounting和countingGuru都是静态成员,所以也可以将该语句放在一个静态块中:static {accounting.add(countingGuru); }'但我不建议这样做。我同意@musical_coder这么多的静态使用是不需要的。我指出这一点仅仅是因为你说你正在学习,这是你可以做的语法。 (并且在某些情况下需要在静态初始化过程中调用方法。) – Radiodef

+0

谢谢。我讨厌承认失败,但我同意我需要花更多的时间在基础知识上。我不想养成坏习惯,把自己投入到我没有做好准备的事情中。 – thatpaintingelephant

1

在语法上,如果更改系的构造函数这个

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

和删除行

accounting.addEmployee(countingGuru); 

那么,你的代码不会出现错误。