[英]Java cannot find constructor
Like most new programmers I have a small but significant issue that i cannot figure out. 像大多数新程序员一样,我有一个很小但很重要的问题,我无法弄清楚。 My program will not pull my constructor. 我的程序不会拉我的构造函数。 I have tried quite a few different ways and cant seem to figure it out. 我尝试了很多不同的方法,似乎无法弄清楚。 Any help would be greatly appreciated. 任何帮助将不胜感激。
Error EmployeeTest.java:13: cannot find symbol symbol : constructor Employee() location: class Employee Employee x = new Employee(); ^ EmployeeTest.java:14: cannot find symbol symbol : constructor Employee() location: class Employee Employee y = new Employee();
public class Employee
{
private double salaryValue; // variable that stores monthlySalary
private String firstName; // instance variable that stores first name
private String lastName; // variable that stores last name
public Employee( String firstNameParameter , String lastNameParameter , double salaryValueParameter )
{
if ( salaryValueParameter < 0.0 ) // validate monthlySalary > 0.0
salaryValue = 0.0; // if not salary is intitalized to default
else
firstName = firstNameParameter;
lastName = lastNameParameter;
salaryValue = salaryValueParameter;
}
public class EmployeeTest
{
public static void main( String[] args )
{
String temp;
Double temp2;
Double temp3;
Employee x = new Employee();
Employee y = new Employee();
Because you've added a constructor that takes 3 arguments, the Employee
class no longer has a default constructor - one which takes no arguments. 因为添加了一个带有3个参数的构造函数,所以Employee
类不再具有默认的构造函数-一个不带参数的构造函数。 So you can't do this: 所以你不能这样做:
Employee x = new Employee();
and you must include 3 arguments: 并且必须包含3个参数:
Employee x = new Employee("firstname", "lastname", 123.45);
If you want to instantiate an Employee
without supplying any parameters, you must add a no-argument constructor: 如果要在不提供任何参数的情况下实例化Employee
,则必须添加一个无参数的构造函数:
public Employee() {
}
You can read more about default constructors in section 8.8.9 of the Java Language Specification . 您可以在Java语言规范的 8.8.9节中阅读有关默认构造函数的更多信息。
You're calling a constructor without any parameters: 您正在调用没有任何参数的构造函数:
new Employee()
This causes Java to look for this default constructor in your class: 这将导致Java在您的类中查找此默认构造函数:
public Employee() {}
Which it can't find because you have a custom constructor with parameters, hence the error. 因为您有一个带有参数的自定义构造函数,所以找不到该错误。 Your Employee
class only has this constructor: 您的Employee
类只有以下构造函数:
public Employee(String, String, double) {}
You should either pass parameters to the constructors in your new
statements, or declare that default parameter-less constructor explicitly and implement it (as an overload of your other constructor, perhaps to pass in default values or something). 您应该在new
语句中将参数传递给构造函数,或者显式声明该默认的无参数构造函数并实现它(作为其他构造函数的重载,也许会传递默认值或某些东西)。
By default, a class has a no-arg constructor. 默认情况下,类具有无参数构造函数。 Ie 即
public Employee()
If you later and go add your own constructor.. ie 如果您以后再去添加自己的构造函数..即
public Employee( String name )
Then the listed ones are the only ones you can use. 然后,列出的那些是您唯一可以使用的。
If you still want to call the default one, add it back in. 如果您仍想调用默认值,请重新添加。
public Employee()
您必须添加一个默认的公共构造函数:
public void Employee();
Your constructor defines String firstNameParameter , String lastNameParameter , double salaryValueParameter 您的构造函数定义了String firstNameParameter,String lastNameParameter,double salvageValueParameter
as parameters. 作为参数。 When you call Employee x = new Employee(); 当您致电Employee x = new Employee(); you are calling a non-existant constructor. 您正在调用不存在的构造函数。 Try the following instead: 请尝试以下操作:
Employee x = new Employee('David', 'Jones', 50000);
You are attempting to create a new class with an empty constructor, but the only constructor you have declared is one requiring parameters. 您正在尝试使用一个空的构造函数创建一个新类,但是您声明的唯一构造函数是一个需要参数的构造函数。 You should probably try something like: 您可能应该尝试以下方法:
Employee x = new Employee("Bob", "Jones", "100.0");
Or, declare a default constructor: 或者,声明一个默认的构造函数:
public Employee() {
//do cool stuff here
}
Hope that helps. 希望能有所帮助。
You provide only a constructor with three arguments, but you try to call a constructor without arguments. 您仅提供具有三个参数的构造函数,但是尝试调用不带参数的构造函数。 Such a constructur is included automatically by the compiler but only if your class has no other constructors. 仅当您的类没有其他构造函数时,编译器才会自动包含此类构造函数。
So you have two options: 因此,您有两种选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.