目前正在使用 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());
}
}
答案 0 :(得分:1)
也许您的导入声明丢失了。将其添加到类名称正下方的类顶部。
import {full package name}.Employee;
答案 1 :(得分:1)
看看这个:
e1_department
和e1_position
应该是字符串对象,但在 java 中没有字符串(请注意名称小写)
通过执行以下操作来修复拼写错误:
String e1_department = "Accounting";
String e1_position = "Vice President";
之后,代码按预期运行:
输出:
答案 2 :(得分:0)
我拿了代码并运行了你的主要功能并得到了这个回复:
Traceback (most recent call last):
File "/usr/bin/subliminal", line 5, in <module>
from pkg_resources import load_entry_point
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3080, in <module>
@_call_aside
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3066, in _call_aside
f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3093, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 651, in _build_master
ws.require(__requires__)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 952, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 839, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'argparse' distribution was not found and is required by stevedore
这里看起来不错。 你能检查一下你是否正确导入了包裹吗?
其次,我在主函数的第二个和第三个字段上看到两个错误: 这些字段被声明为带有小s的字符串,应该是String。
答案 3 :(得分:0)
我刚刚尝试编译你的程序,它最初没有工作。原因是您在ChallengeImplementor字段中使用了原始字符串类型:
string e1_department = "Accounting";
string e1_position = "Vice President";
一旦我将其更改为String,它编译并正常工作: screenshot。 右键单击ChallengeImplementor.class并将其作为Java Application
启动希望这有帮助
答案 4 :(得分:0)
好吧,我是一个完全白痴。不知何故,Employee.java文件被移出同一目录。我认为jGrasp意外地将它保存在其他地方,即使这两个文件最初都是在哪里。
我修复了String问题 - 这实际上只是我试图执行的测试,而且从来不需要代码。