2013-10-24 48 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); 

那麼,你的代碼不會出現錯誤。