为什么我会收到错误消息’cannot find symbol’?

huangapple go评论70阅读模式
英文:

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(&quot;gopi&quot;);

    t.name=&quot;ravi&quot;;
    t.eat();
    t.walk();
    t.teach();
    Singer s=new Singer(&quot;rock&quot;);
    s.name=&quot;arjun&quot;;
    s.eat();
    s.walk();

    person p =new person(&quot;jack&quot;);
    //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(&quot;gopi&quot;);
    ^
  symbol:   class Teacher

  location: class Inheritance


Inheritance.java:5: error: cannot find symbol

        Teacher t=new Teacher(&quot;gopi&quot;);
                      ^

  symbol:   class Teacher

  location: class Inheritance

Inheritance.java:10: error: cannot find symbol

        Singer s=new Singer(&quot;rock&quot;);
        ^

  symbol:   class Singer

  location: class Inheritance

Inheritance.java:10: error: cannot find symbol

        Singer s=new Singer(&quot;rock&quot;);
                     ^

  symbol:   class Singer

  location: class Inheritance

Inheritance.java:15: error: cannot find symbol

        person p =new person(&quot;jack&quot;);
        ^

  symbol:   class person

  location: class Inheritance

Inheritance.java:15: error: cannot find symbol

        person p =new person(&quot;jack&quot;);
                      ^
  
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(&quot;Inside person constructor&quot;);   
  }

  public void walk(){
  System.out.println(&quot;person&quot;+name+&quot;person is walking&quot;);
}
  public void eat(){
  System.out.println(&quot;person&quot;+name+&quot;person is eating&quot;);
 }
  public static void laughing(){
  System.out.println(&quot;person is laughing&quot;);
     }

  }
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(&quot;Inside teacher constructor&quot;);   
  }

   public void teach(){
   System.out.println(name+&quot;Teacher is teaching&quot;);
   }

   public void eat(){
   super.eat();//to access the parent class i.e, here person           class
   System.out.println(&quot;teacher&quot;+name+&quot;is eating&quot;);
     }
    }
   }

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(&quot;Inside singer constructor&quot;);   

  }

    public void sing(){
    System.out.println(&quot;Singer is singing&quot;);
}

    public void eat(){
    System.out.println(&quot;teacher&quot;+name+&quot;is eating&quot;);
}

}

I&#39;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可以正常工作:

&gt;     $ ls oops/inheritance/
&gt;     Inheritance.java	Singer.java
&gt;     Person.java		Teacher.java
&gt;     $ javac oops/inheritance/Person.java 
&gt;     $ javac oops/inheritance/Teacher.java oops/inheritance/Singer.java 
&gt;     $ javac oops/inheritance/Inheritance.java 
&gt;     $ ls oops/inheritance/
&gt;     Inheritance.class	Person.java		Teacher.class
&gt;     Inheritance.java	Singer.class		Teacher.java
&gt;     Person.class		Singer.java
&gt;     $ java oops.inheritance.Inheritance
&gt;     在Person构造函数内部
&gt;     在teacher构造函数内部
&gt;     Personraviperson在吃饭
&gt;     teacherravi在吃饭
&gt;     Personraviperson在走路
&gt;     raviteacher正在教学
&gt;     在Person构造函数内部
&gt;     在singer构造函数内部
&gt;     teacherarjun在吃饭
&gt;     Personarjunperson在走路
&gt;     在Person构造函数内部
&gt;     $

与你的代码相比,我在包名`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&#39;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:

&gt;     $ ls oops/inheritance/
&gt;     Inheritance.java	Singer.java
&gt;     Person.java		Teacher.java
&gt;     $ javac oops/inheritance/Person.java 
&gt;     $ javac oops/inheritance/Teacher.java oops/inheritance/Singer.java 
&gt;     $ javac oops/inheritance/Inheritance.java 
&gt;     $ ls oops/inheritance/
&gt;     Inheritance.class	Person.java		Teacher.class
&gt;     Inheritance.java	Singer.class		Teacher.java
&gt;     Person.class		Singer.java
&gt;     $ java oops.inheritance.Inheritance
&gt;     Inside Person constructor
&gt;     Inside teacher constructor
&gt;     Personraviperson is eating
&gt;     teacherraviis eating
&gt;     Personraviperson is walking
&gt;     raviTeacher is teaching
&gt;     Inside Person constructor
&gt;     Inside singer constructor
&gt;     teacherarjunis eating
&gt;     Personarjunperson is walking
&gt;     Inside Person constructor
&gt;     $

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
  |
  -&gt;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&gt; javac inheritance/person.java    
PS E:\Java&gt; javac inheritance/teacher.java  
PS E:\Java&gt; javac inheritance/MainClass.java
PS E:\Java&gt; java inheritance/Mainclass

This will solve your problem

Happy Coding!!

huangapple
  • 本文由 发表于 2020年5月29日 11:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定