英文:
why do i get the error as 'cannot find symbol'?
问题
Inheritance.java 文件
package oops.Inheritance;
public class Inheritance {
public static void main(String[] args) {
Teacher t = new Teacher("gopi");
t.name = "ravi";
t.eat();
t.walk();
t.teach();
Singer s = new Singer("rock");
s.name = "arjun";
s.eat();
s.walk();
person p = new person("jack"); // person 类的对象
//person p = t; // 向上转型
//Teacher t = (Teacher) p; // 向下转型
// boolean yo = t instanceof Teacher; // 判断 t 是否是 Teacher 类的实例
// System.out.println(t instanceof Teacher); // true
// System.out.println(s instanceof Singer); // true
// System.out.println(t instanceof person); // true
// System.out.println(p instanceof Teacher); // false
}
}
错误信息
D:\study files\java files\oops\Inheritance>javac Inheritance.java
Inheritance.java:5: 错误: 找不到符号
Teacher t = new Teacher("gopi");
^
符号: 类 Teacher
位置: 类 Inheritance
Inheritance.java:5: 错误: 找不到符号
Teacher t = new Teacher("gopi");
^
符号: 类 Teacher
位置: 类 Inheritance
Inheritance.java:10: 错误: 找不到符号
Singer s = new Singer("rock");
^
符号: 类 Singer
位置: 类 Inheritance
Inheritance.java:10: 错误: 找不到符号
Singer s = new Singer("rock");
^
符号: 类 Singer
位置: 类 Inheritance
Inheritance.java:15: 错误: 找不到符号
person p = new person("jack");
^
符号: 类 person
位置: 类 Inheritance
Inheritance.java:15: 错误: 找不到符号
person p = new person("jack");
^
符号: 类 person
位置: 类 Inheritance
6 个错误
person.java 文件
package oops.Inheritance;
public class person {
protected String name;
public person(String name) {
this.name = name;
System.out.println("Inside person constructor");
}
public void walk() {
System.out.println("person" + name + "正在走路");
}
public void eat() {
System.out.println("person" + name + "正在吃饭");
}
public static void laughing() {
System.out.println("person 正在笑");
}
}
Teacher.java 文件
package oops.Inheritance;
public class Teacher extends person { // 继承自 person
public Teacher(String name) {
super(name); // 调用父类的构造函数
System.out.println("Inside teacher constructor");
}
public void teach() {
System.out.println(name + "Teacher 正在教学");
}
public void eat() {
super.eat(); // 访问父类,即 person 类的方法
System.out.println("teacher" + name + "正在吃饭");
}
}
singer.java 文件
package oops.Inheritance;
public class Singer extends person { // 继承自 person
public Singer(String name) {
super(name); // 调用父类的构造函数
System.out.println("Inside singer constructor");
}
public void sing() {
System.out.println("Singer 正在唱歌");
}
public void eat() {
System.out.println("teacher" + name + "正在吃饭");
}
}
我在最新版本的 VSCode 中运行这个程序。每次当我从其他包中导入类时,就会出现上述错误。
<details>
<summary>英文:</summary>
Inheritance.java file
package oops.Inheritance;
public class Inheritance {
public static void main(String[] args) {
Teacher t=new Teacher("gopi");
t.name="ravi";
t.eat();
t.walk();
t.teach();
Singer s=new Singer("rock");
s.name="arjun";
s.eat();
s.walk();
person p =new person("jack");
//person p=t;//upcasting
//Teacher t=(Teacher)p;//downcasting
// boolean yo = t instanceof Teacher;//to fine whether t is is instance of teacher
// System.out.println(t instanceof Teacher);//true
// System.out.println(s instanceof Singer);//true
// System.out.println(t instanceof person);//true
// System.out.println(p instanceof Teacher);//flase
}
}
The error is
D:\study files\java files\oops\Inheritance>javac Inheritance.java
Inheritance.java:5: error: cannot find symbol
Teacher t=new Teacher("gopi");
^
symbol: class Teacher
location: class Inheritance
Inheritance.java:5: error: cannot find symbol
Teacher t=new Teacher("gopi");
^
symbol: class Teacher
location: class Inheritance
Inheritance.java:10: error: cannot find symbol
Singer s=new Singer("rock");
^
symbol: class Singer
location: class Inheritance
Inheritance.java:10: error: cannot find symbol
Singer s=new Singer("rock");
^
symbol: class Singer
location: class Inheritance
Inheritance.java:15: error: cannot find symbol
person p =new person("jack");
^
symbol: class person
location: class Inheritance
Inheritance.java:15: error: cannot find symbol
person p =new person("jack");
^
symbol: class person
location: class Inheritance
6 errors
person.java
package oops.Inheritance;
public class person {
protected String name;
public person(String name){
this.name=name;
System.out.println("Inside person constructor");
}
public void walk(){
System.out.println("person"+name+"person is walking");
}
public void eat(){
System.out.println("person"+name+"person is eating");
}
public static void laughing(){
System.out.println("person is laughing");
}
}
Teacher.java
package oops.Inheritance;
public class Teacher extends person{//inheriting from person
public Teacher(String name){
super(name);//calls the constructor in the parent class
System.out.println("Inside teacher constructor");
}
public void teach(){
System.out.println(name+"Teacher is teaching");
}
public void eat(){
super.eat();//to access the parent class i.e, here person class
System.out.println("teacher"+name+"is eating");
}
}
}
singer.java
package oops.Inheritance;
public class Singer extends person{//inheriting from person
public Singer(String name){
super(name);//calls the the constructor in parent class
System.out.println("Inside singer constructor");
}
public void sing(){
System.out.println("Singer is singing");
}
public void eat(){
System.out.println("teacher"+name+"is eating");
}
}
I'm running this program in vscode the latest version.
every time it works but when I import the classes from the other package I get the above-mentioned error.
</details>
# 答案1
**得分**: 2
以下是翻译好的内容:
你能做的最好的事情就是在Eclipse中修复设置,使其正常工作。一旦解决了这个问题,你就不需要再担心如何编译。
无论如何,为了回答你提出的问题:
为了从命令行编译你的文件,你需要在`java files`目录中。这必须是你的工作目录,因为它是包含最外层包`oops`的根目录。
然后你需要首先编译`person`。你不能单独编译`Inheritance`。在每个类被编译之后,编译器才会识别在`Inheritance`中使用的类——`person`、`Teacher`和`Singer`。使用相对文件路径名:
javac oops/Inheritance/person.java
(如果在Windows上,请使用反斜杠而不是正斜杠)。在编译`person`之后,编译`Teacher`和`Singer`(顺序无关紧要)。最后编译`Inheritance`。
**编辑:** 在我的BSD Unix系统上,使用bash可以正常工作:
> $ ls oops/inheritance/
> Inheritance.java Singer.java
> Person.java Teacher.java
> $ javac oops/inheritance/Person.java
> $ javac oops/inheritance/Teacher.java oops/inheritance/Singer.java
> $ javac oops/inheritance/Inheritance.java
> $ ls oops/inheritance/
> Inheritance.class Person.java Teacher.class
> Inheritance.java Singer.class Teacher.java
> Person.class Singer.java
> $ java oops.inheritance.Inheritance
> 在Person构造函数内部
> 在teacher构造函数内部
> Personraviperson在吃饭
> teacherravi在吃饭
> Personraviperson在走路
> raviteacher正在教学
> 在Person构造函数内部
> 在singer构造函数内部
> teacherarjun在吃饭
> Personarjunperson在走路
> 在Person构造函数内部
> $
与你的代码相比,我在包名`inheritance`中使用了小写的`i`,在类名`Person`中使用了大写的`P`,都符合Java的命名约定。我还纠正了大括号的闭合数量和其他微小的细节,这些与编译和运行问题无关。
当你尝试我的建议时出了什么问题,很抱歉,我无法猜测。
<details>
<summary>英文:</summary>
The best thing you can do is to fix the setup in Eclipse so that that works. Once that problem is solved, you don't need to worry about how to compile anymore.
In any case, to answer the question you asked:
In order to compile your files from the command line, you need to be in the `java files` directory. This needs to be your working directory since it is the root directory containing your outermost package, `oops`.
Then you need to compile `person` first. You cannot compile `Inheritance` alone. The compiler will only recognize the classes used in `Inheritance` — `person`, `Teacher` and `Singer` — after each of them has been compiled. Use the relative path name to the file:
javac oops/Inheritance/person.java
(use backslash instead of slash if on Windows). After `person` compile `Teacher` and `Singer` (in any order). Finally compile `Inheritance`.
**Edit:** This worked on my BSD Unix with bash:
> $ ls oops/inheritance/
> Inheritance.java Singer.java
> Person.java Teacher.java
> $ javac oops/inheritance/Person.java
> $ javac oops/inheritance/Teacher.java oops/inheritance/Singer.java
> $ javac oops/inheritance/Inheritance.java
> $ ls oops/inheritance/
> Inheritance.class Person.java Teacher.class
> Inheritance.java Singer.class Teacher.java
> Person.class Singer.java
> $ java oops.inheritance.Inheritance
> Inside Person constructor
> Inside teacher constructor
> Personraviperson is eating
> teacherraviis eating
> Personraviperson is walking
> raviTeacher is teaching
> Inside Person constructor
> Inside singer constructor
> teacherarjunis eating
> Personarjunperson is walking
> Inside Person constructor
> $
Compared to your code I have used small `i` in the package name `inheritance` and upper case `P` in the class `Person`, both in accordance with Java naming conventions. I have also corrected the number of closing curly braces and similar tiny details that aren’t relevant to the problem of compiling and running.
What went wrong for you when you tried my suggestion, I am sorry, I cannot guess.
</details>
# 答案2
**得分**: 2
希望你正在学习 Apni Kaksha 的 Java 教程,我已经帮你解决了问题,下面是我的目录结构:
```plaintext
根目录
|
->inheritance
|
person.java
|
teacher.java
|
MainClass.java
所以,在根目录中(而不是在 inheritance 目录中),你需要执行以下命令:
PS E:\Java> javac inheritance/person.java
PS E:\Java> javac inheritance/teacher.java
PS E:\Java> javac inheritance/MainClass.java
PS E:\Java> java inheritance/Mainclass
这将解决你的问题。
编码愉快!
英文:
Hope you are going through Apni Kaksha Java Tustorials, here I've got your problem solved, below is my directory Structure
root
|
->inheritance
|
person.java
|
teacher.java
|
MainClass.java
so what you need to do, being in root directory (not in inheritance directory)
run following commands
PS E:\Java> javac inheritance/person.java
PS E:\Java> javac inheritance/teacher.java
PS E:\Java> javac inheritance/MainClass.java
PS E:\Java> java inheritance/Mainclass
This will solve your problem
Happy Coding!!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论