我正在尝试创建从同一个超类继承的多个类。我的第一堂课表现很好。当我尝试第二次尝试时,我收到一条以前未曾见过的错误消息。我是新来的,所以不确定如何解决问题。
这是课程
class Employee extends Person{
double salary;
private java.util.Date dateCreated;
public Employee(){
}
public Employee(String name, String address, String phone_number, double salary){
super(name, address, phone_number);
this.salary = salary;
}
public String getDate(){
java.util.Date date = new java.util.Date();
return date.toString();
}
public double getSalary(){
return this.salary;
}
public String toString(){
return("Employee object:\n" + super.toString() + "\n" + "Salary:\t$" + getSalary() + "\n" + "Date Hired: " + getDate());
}
}
我正在尝试以这种方式调用该方法
Employee employee = new Employee (name, address, number, salary);
System.out.println(employee.toString());
我收到此错误消息:
testPerson.java:21: error: constructor Employee in class Employee cannot be applied to given types;
Employee employee = new Employee (name, address, number, salary);
^
required: no arguments
found: String,String,String,double
reason: actual and formal argument lists differ in length
1 error
这是TestPerson类
public class TestPerson{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter your name: ");
String name = input.next();
System.out.println("Please enter your address: ");
String address = input.next();
System.out.println("Please enter your phone number: ");
String number = input.next();
System.out.println("Please enter your yearly salary: ");
double salary = input.nextDouble();
Employee employee = new Employee (name, address, number, salary);
System.out.println(employee.toString());
}
}
这是要清除它的父类。我可能做错了一些我没发现的错误。
import java.util.*;
public class Person{
private String name;
private String address;
private String phone_number;
public Person(){
}
public Person(String name, String address, String phone_number){
this.name = name;
this.address = address;
this.phone_number = phone_number;
}
public String getName(){
return this.name;
}
public String getAddress(){
return this.address;
}
public String getPhone(){
return this.phone_number;
}
public String toString(){
return ("Person object:\n" + "Name:\t\t" + getName() + "\n" + "Address:\t" + getAddress() + "\n" + "Phone#:\t" + getPhone());
}
}
答案 0 :(得分:0)
从现在开始,我一直在关注此问题的状态,我发现它真的很有趣,因此我复制了您的代码并在本地对其进行了测试,令人惊讶,就像我期望的那样……没有发现任何错误。 看来您的编译器正在尝试使用不带参数的构造函数,而不是您提供的第二个参数。如果这确实是testPerson.java中使用的Employee类,则应该验证您的代码。