像大多数新程序员一样,我有一个我无法解决的小但重要的问题。
我的程序不会拉动我的构造函数。我尝试了很多不同的方法,但似乎无法弄清楚。任何帮助将不胜感激。
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();
因为您添加了一个带有 3 个参数的构造函数,Employee
类不再具有默认构造函数 - 不带参数的构造函数。所以你不能这样做:
Employee x = new Employee();
并且您必须包含 3 个参数:
Employee x = new Employee("firstname", "lastname", 123.45);
如果你想实例化一个Employee
在不提供任何参数的情况下,您必须添加一个无参数构造函数:
public Employee() {
}
您可以阅读有关默认构造函数的更多信息第8.8.9节 https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.8.9 of the Java语言规范 https://docs.oracle.com/javase/specs/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)