1

我有员工和公司两个班级。我需要按年龄升序列出部门名称+员工姓名+年龄。我得到了这个类型 无法实例化类型 Employee Company.java (我把* *放在错误上)。这是我的员工和公司课程。我不知道将员工类和公司类联系起来有什么问题。

public abstract class Employee implements Comparable<Employee>{
    private String name;
    private int age;
    PrimeAgeChecker checks;
    Department department;

    Employee(Department department,String name, int age)
    {
        this.department = department;
        this.name = name;
        this.age = age;
        this.checks = new PrimeAgeChecker(this);
    }


    public int compareTo(Employee x){
        if (this.age < x.age)
            return -1;
        else if (this.age > x.age)
            return 1;
        else 
            return 0;
    }

    public int getAge()
    {
        return age;
    }

    public String getName()
    {
        return name;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(getDept(department));
        sb.append("\t");
        sb.append(getName());
        sb.append("\t");
        sb.append(getAge());
        sb.append("\t");
        sb.append(checks.check());

        return sb.toString();
    }

    private Department getDept(Department department){
        return department;
    }

}





import java.util.ArrayList;
import java.util.*;

public class Company {

    Department department;

    private ArrayList<Employee> directory = new ArrayList<Employee>();


            public void addEmployee(Employee emplo) {
                directory.add(emplo);
            }

            public void sortBySalary() {
                // Bubble sort
                for (int i = 0; i < directory.size(); i++) {
                    for (int j = directory.size() - 1; j > i; j--) {
                        if (directory.get(j - 1).compareTo(directory.get(j)) > 0) {
                            Employee temp = directory.get(j);
                            directory.set(j, directory.get(j - 1));
                            directory.set(j - 1, temp);
                        }
                    }
                }
            }

            public void listEmployees() {
                for (Employee emplo : directory) 
                    System.out.println(emplo.toString());
                System.out.println();
            }

            public static void main(String[] args) {    
                Company direct = new Company();

                direct.addEmployee(new ***Employee***(Department.Accounting,"Counting Guru",55));
                direct.addEmployee(new ***Employee***(Department.Accounting,"Counting Pro", 45));
                direct.addEmployee(new ***Employee***(Department.Accounting,"Counting Savvy", 40));
                direct.addEmployee(new ***Employee***(Department.Accounting,"Counting Novice", 25));
                direct.addEmployee(new ***Employee***(Department.Marketing,"Sales Guru", 50));
                direct.addEmployee(new ***Employee***(Department.Marketing,"Sales Pro", 48));
                direct.addEmployee(new ***Employee***(Department.Marketing,"Sales Savvy", 38));
                direct.addEmployee(new ***Employee***(Department.Human_Resources,"Hiring Guru", 58));
                direct.addEmployee(new ***Employee***(Department.Human_Resources,"Hiring Pro", 47));
                direct.addEmployee(new ***Employee***(Department.Information_Systems,"Hacking Pro", 46));
                direct.addEmployee(new ***Employee***(Department.Information_Systems,"Hacking Guru", 51));
                direct.addEmployee(new ***Employee***(Department.Information_Systems,"Hacking Savvy", 38));
                direct.addEmployee(new ***Employee***(Department.Information_Systems,"Hacking Novice", 23));

                direct.listEmployees();
                direct.sortBySalary();
                System.out.println("Department\t\tName\tAge\tPrime");
                System.out.println("__________________________________________________");
                direct.listEmployees();
            }


}
4

1 回答 1

3

Employee是一个abstract class,它不能被实例化。

抽象类是被声明为抽象的类——它可能包含也可能不包含抽象方法。抽象类不能被实例化,但它们可以被子类化。

如果要创建员工,则必须定义一个扩展 Employee 的子类:

class ConcreteEmployee extends Employee{    
    ConcreteEmployee(Department d, String name, int age) {
        super(d, name, age);
    }
}

然后

direct.addEmployee(new ConcreteEmployee(Department.Accounting,"Counting Guru",55));
于 2013-10-24T07:12:51.813 回答