我是Java的新手,我的任务是设置两个类。一个是我做过的人,编译没有错误。第二个是教师,它添加了一个名为subject的变量。当我尝试编译我的Teacher类时,我得到了Teacher.java:3:error:找不到符号,当我去工具并使用Evaluate Line时,它给出了错误(1):函数未定义消息。
这是我的Person Class代码:
包学校;
public abstract class Person {
private String firstName;
private String lastName;
public Person() {
}
public Person(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() { return lastName; }
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return firstName + " " + lastName;
}
}
这是我遇到编译错误的Teacher类的代码:
package School;
public class Teacher extends Person {
/*error(1): function not defined */
private String subject;
public Teacher() {
}
public Teacher(String firstName, String lastName) {
super(firstName, lastName);
}
public Teacher(String firstName,String lastName, String subject) {
super(firstName, lastName);
setSubject(subject);
}
public String getSubject() { return subject; }
public void setSubject(String subject) {
this.subject = subject;
}
public String getFullName() {
return getFullName() + "Roger" + "Sakowski " + "teaches "+ subject + "English ";
}
}
我搜索了互联网和我的书籍,无法弄清楚我做错了什么。
答案 0 :(得分:0)
在使用package
语句编译程序时,必须使用-d选项。换句话说,您必须在具有这两个文件的目录中进行如下编译:
javac -d {PUT THE PATH TO THE FOLDER THAT HAS THE FILES HERE, WITHOUT THE BRACKETS} Person.java
javac -d {PUT THE PATH TO THE FOLDER THAT HAS THE FILES HERE, WITHOUT THE BRACKETS} Teacher.java
这样编译器就能找到Person
类。将创建具有给定包名称(在本例中为School
)的文件夹。该文件夹将包含已编译的.class文件。