0 java
像大多数新程序员一样,我有一个小但重要的问题,我无法弄清楚.我的程序不会拉我的构造函数.我尝试了很多不同的方法,似乎无法弄明白.任何帮助将不胜感激.
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();
Run Code Online (Sandbox Code Playgroud)
Ric*_*arn 10
因为您添加了一个带有3个参数的构造函数,所以Employee该类不再具有默认构造函数 - 一个不带参数的构造函数.所以你不能这样做:
Employee x = new Employee();
Run Code Online (Sandbox Code Playgroud)
并且您必须包含3个参数:
Employee x = new Employee("firstname", "lastname", 123.45);
Run Code Online (Sandbox Code Playgroud)
如果要在Employee不提供任何参数的情况下实例化,则必须添加无参数构造函数:
public Employee() {
}
Run Code Online (Sandbox Code Playgroud)
您可以在Java语言规范的8.8.9节中阅读有关默认构造函数的更多信息.
| 归档时间: |
|
| 查看次数: |
6198 次 |
| 最近记录: |