【问题标题】:why do i get the error as 'cannot find symbol'?为什么我会收到“找不到符号”的错误?
【发布时间】:2020-05-29 03:08:42
【问题描述】:

继承.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 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



    }
}

错误是

D:\study files\java files\oops\Inheritance>javac Inheritance.java

Inheritance.java:5: 错误:找不到符号

        Teacher t=new Teacher("gopi");
        ^

符号:班主任

位置:类继承

Inheritance.java:5: 错误:找不到符号

    Teacher t=new Teacher("gopi");
                  ^

符号:班主任

位置:类继承

Inheritance.java:10: 错误:找不到符号

    Singer s=new Singer("rock");
    ^

符号:歌手类

位置:类继承

Inheritance.java:10: 错误:找不到符号

    Singer s=new Singer("rock");
                 ^

符号:歌手类

位置:类继承

Inheritance.java:15: 错误:找不到符号

    person p =new person("jack");
    ^

符号:类人

位置:类继承

Inheritance.java:15: 错误:找不到符号

    person p =new person("jack");
                  ^

符号:类人

位置:类继承

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+"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");
    }
}

我在最新版本的 vscode 中运行这个程序。 每次它都可以工作,但是当我从另一个包中导入类时,我会收到上述错误。

【问题讨论】:

  • 所有这些类都在同一个包中吗?如果没有,您是否导入了它们?
  • 您在错误的目录中。您应该位于包层次结构的根部。 cd .. 并使用 javac Inheritance/Inheritance.java
  • javac时应该在java files目录下。
  • @OP 你的评论毫无意义。在执行之前,您无法得到“找不到主类”错误,并且在编译之前无法执行,并且目前无法编译。您仍然在错误的目录中。您应该在java files 目录中,即cd ..\.. 并发出javac oop/Inheritance/Inheritance.java。然后java oop.Inheritance.Inheritance.
  • 所有这些源文件都在D:\study files\java files\oops\Inheritance 中吗?如果不是,为什么不呢?在您的问题中,“每次都有效”到底是什么意思?

标签: java oop inheritance


【解决方案1】:

您可以做的最好的事情是修复 Eclipse 中的设置,以使其正常工作。一旦解决了这个问题,你就不必再担心如何编译了。

无论如何,回答你提出的问题:

为了从命令行编译您的文件,您需要位于java files 目录中。这必须是您的工作目录,因为它是包含最外层包的根目录,oops

那你需要先编译person。你不能单独编译Inheritance。编译器只会识别Inheritance 中使用的类——personTeacherSinger——在它们中的每一个都被编译之后。使用文件的相对路径名:

javac oops/Inheritance/person.java

(如果在 Windows 上使用反斜杠而不是斜杠)。在person 之后编译TeacherSinger(以任何顺序)。最后编译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
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
$

与您的代码相比,我在包名inheritance 中使用了小写i,在Person 类中使用了大写P,两者都符合Java 命名约定。我还更正了右花括号的数量以及与编译和运行问题无关的类似小细节。

当你尝试我的建议时,你出了什么问题,对不起,我猜不出来。

【讨论】:

  • 正如你所说,我先编译了人,然后当我编译教师课程时,我得到错误 asD:\study files\java files>javac oops/Inheritance/person.java D:\study files\java files >javac oops/Inheritance/teacher.java oops\Inheritance\teacher.java:3: error: cannot find symbol public class Teacher extends person{//inheriting from person ^ symbol: class person 1 error
  • 我试过 eclipse 但我得到了这个i.imgur.com/XypBk57.png
  • 我看到了您的错误消息找不到符号,但我无法重现它。如我的编辑所示。对不起。
  • 一个疯狂的猜测:在您的 Java 代码中,您有 Teacher 和大写 T。在您的命令行javac oops/Inheritance/teacher.java 上,您有一个小的t。虽然 Windows 不介意,但我不知道 javac 是否介意。另外Windows如何显示/打印文件名,大写或小写T
  • 我在编译 Teacher.java 时使用了“T”,但它仍然不起作用
【解决方案2】:

希望您通过 Apni Kaksha Java Tustorials,我已经解决了您的问题,下面是我的目录结构

root
  |
  ->inheritance
    |
    person.java
    |
    teacher.java
    |
    MainClass.java

所以你需要做的是,在根目录中(而不是在继承目录中) 运行以下命令

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

这会解决你的问题

编码愉快!!

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2021-09-26
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多