Java 施工人员拒绝使用
目前使用jgrasp编写一个小程序,使用同一目录中的两个不同文件。在一个文件中,我有一个公共类,其中有几个名为Employee的公共构造函数。在另一个文件中,我有一个用于实现Employee类的类。我无法构建这个程序-编译器本质上告诉我,这样提供的构造函数不存在,请参见下面的代码。因此,我所做的每一次构建都失败了 Employee类中的构造函数具有公共访问权限,据我所知,它们的名称是正确的。我是一个C类的人,所以我不知道我是否只是因为我的性格或什么而看不到这个问题。我现在在学校学习Java 这是我的员工课程:Java 施工人员拒绝使用,java,constructor,jgrasp,Java,Constructor,Jgrasp,目前使用jgrasp编写一个小程序,使用同一目录中的两个不同文件。在一个文件中,我有一个公共类,其中有几个名为Employee的公共构造函数。在另一个文件中,我有一个用于实现Employee类的类。我无法构建这个程序-编译器本质上告诉我,这样提供的构造函数不存在,请参见下面的代码。因此,我所做的每一次构建都失败了 Employee类中的构造函数具有公共访问权限,据我所知,它们的名称是正确的。我是一个C类的人,所以我不知道我是否只是因为我的性格或什么而看不到这个问题。我现在在学校学习Java 这是
public class Employee
{
// description: class representation of an employee.
// variable declarations
String name = "", department = "", position = "";
int idNumber = 0;
// constructors
public Employee(String n, String d, String p, int id)
{
// full constructor
this.name = n;
this.department = d;
this.position = p;
if (id > -1) this.idNumber = id;
else this.idNumber = 0;
}
public Employee(String n, int id)
{
// partial constructor (name and id)
this.name = n;
if (id > -1) this.idNumber = id;
else idNumber = 0;
this.department = "";
this.position = "";
}
public Employee()
{
// default constructor
this.name = "";
this.department = "";
this.position = "";
this.idNumber = 1;
}
// accessors
public String getName() { return name; }
public String getDepartment() { return department; }
public String getPosition() { return position; }
public int getID() { return idNumber; }
// mutators
public void setName(String newName) { name = newName; }
public void setDepartment(String newDepartment) { department = newDepartment; }
public void setPosition(String newPosition) { position = newPosition; }
public void setID(int newID) { idNumber = newID; }
}
这是我的实现类:
public class ChallengeImplementor
{
public static void main(String[] args)
{
// create instance-variables for constructors
String e1_name = "Susan Meyers", e2_name = "Mark Jones";
string e1_department = "Accounting";
string e1_position = "Vice President";
int e1_ID = 47899, ew_ID = 39119;
// create several employee objects to prove they work.
Employee e1 = new Employee("Susan Meyers", "Accounting", "Vice President", 47899), // created one with full constructor
e2 = new Employee("Mark Jones", 39119), // ... partial constructor
e3 = new Employee(); // ... default constructor
// set values for e2 that weren't covered in partial constructor
e2.setDepartment("IT");
e2.setPosition("Programmer");
// set values for e3
e3.setName("Joy Rogers");
e3.setDepartment("Manufacturing");
e3.setPosition("Engineer");
e3.setID(81774);
// display all three employees
System.out.println("Name\t\tID\t\tDepartment\t\tPosition");
System.out.println("----------------------------------------------------------------------");
System.out.println(e1.getName() + "\t" + e1.getID() + "\t\t" + e1.getDepartment() + "\t\t" + e1.getPosition());
System.out.println(e2.getName() + "\t" + e2.getID() + "\t\t" + e3.getDepartment() + "\t\t" + e2.getPosition());
System.out.println(e3.getName() + "\t" + e3.getID() + "\t\t" + e3.getDepartment() + "\t\t" + e3.getPosition());
}
}
也许你的进口声明丢失了。将其添加到类的顶部,就在包名称的下方
import {full package name}.Employee;
看看这个:
e1_department和e1_position应该是字符串对象,但在java中没有字符串,请将名称记为小写
通过执行以下操作修复输入错误:
String e1_department = "Accounting";
String e1_position = "Vice President";
在此之后,代码将按预期运行:
输出:
我获取了代码并运行了主函数,得到了以下响应:
Name ID Department Position
----------------------------------------------------------------------
Susan Meyers 47899 Accounting Vice President
Mark Jones 39119 Manufacturing Programmer
Joy Rogers 81774 Manufacturing Engineer
这里看起来不错。
你能检查一下你是否正确导入了这个包吗
其次,我在主函数的第二个和第三个字段中看到两个错误:
字段是用小s声明的字符串,应该是String。我刚刚尝试编译您的程序,但一开始它不起作用。原因是您对ChallengeImplementor字段使用了基本字符串类型:
string e1_department = "Accounting";
string e1_position = "Vice President";
一旦我将其更改为字符串,它就可以编译并正常工作:
.
右键单击ChallengeImplementor.class并将其作为Java应用程序启动
希望这有帮助好吧,我是个十足的白痴。不知怎的,Employee.java文件被移出了同一目录。我认为jGrasp意外地将它保存在了其他地方,即使这两个文件最初都是从那里开始的
我修复了字符串问题——实际上,这只是我试图执行的一个测试,代码从来都不需要 添加确切的编译器错误只是一个旁注,但您可能希望查看构造函数重载。这是否会导致提供的构造函数不存在的警告?如果它不在同一个包中,您可以通过编写导入语句来使用它。