我该如何解决这些错误?

时间:2014-04-29 23:28:01

标签: java inheritance

public class EmployeeTest {
  public static void main(String args[]) {
    Employee e = new Employee ("Jordan",70000.00);
    Manager m = new Manager ("William Johnson",90000.00,"Computer Science");
    Executive ex = new Executive ("GPC",120000.00,"School");

    System.out.println(e);
    System.out.println(m);
    System.out.println(ex);
  }

这是我的构造函数:

public class Employee {
  private String name;
  private double salary;

  public Employee(String name, double salary, int department) {
    setName(name);
    setSalary(salary);
  }
  public String getName() {
    return this.name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public double getSalary() {
    return this.salary;
  }
  public void setSalary(double salary) {
    this.salary = salary;
  }
  public void getDetails() {
    System.out.println("Name:" + getName());
    System.out.println("Salary:" +getSalary());
  }
}

我目前很难接受这项任务。我用Employee实现了所有四个类。然后,Manager从Employee继承,同时还从Manager继承执行。当我去编译它时,我发现了这两个错误: 我做错了什么?

File: C:\Users\Jordan\Downloads\EmployeeTest.java  [line: 6]
Error: constructor Employee in class Employee cannot be applied to given types;
  required: java.lang.String,double,int
  found: java.lang.String,double
  reason: actual and formal argument lists differ in length
File: C:\Users\Jordan\Downloads\Manager.java  [line: 9]
Error: constructor Employee in class Employee cannot be applied to given types;
  required: java.lang.String,double,int
  found: java.lang.String,double
  reason: actual and formal argument lists differ in length

2 个答案:

答案 0 :(得分:3)

您需要将一个额外的参数传递给员工创建调用,如下所示:

Employee e = new Employee ("Jordan",70000.00,42);

您已将构造函数定义为Stringdoubleint,因此您必须提供所有三个。

答案 1 :(得分:1)

您的输出明确说明

Error: constructor Employee in class Employee cannot be applied to given types;
    required: java.lang.String,double,int
    found: java.lang.String,double

因此,您需要将三个参数传递给Employee e = new Employee ("Jordan",70000.00)String, Double,而不是调用int构造函数。例如,

Employee e = new Employee ("Jordan",70000.00, 1);   //I passed 1 as args for example it could be anything that you defined in your code.

修改的 你去了,你刚刚发布了你的Employee类代码,它清楚地表明你创建了一个带有三个参数String name, double salary, int department的构造函数,看起来好像你完全忘记了你想用第三个参数做什么{ {1}}因为我没有看到它在代码中的任何其他地方使用过。