我为员工和公司有两个班级,我需要按年龄升序列出部门名称+员工名称+年龄。我收到此类型无法实例化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();
}
}
最佳答案
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));
关于java - Type无法实例化Employee Company.java类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19559275/