簡體   English   中英

為什么我會收到“找不到符號”的錯誤?

[英]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 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");
        ^

符號:class 老師

位置: class Inheritance

Inheritance.java:5:錯誤:找不到符號

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

符號:class 老師

位置: class Inheritance

Inheritance.java:10:錯誤:找不到符號

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

符號:class 歌手

位置: class Inheritance

Inheritance.java:10:錯誤:找不到符號

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

符號:class 歌手

位置: class Inheritance

Inheritance.java:15:錯誤:找不到符號

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

符號:class 人

位置: class Inheritance

Inheritance.java:15:錯誤:找不到符號

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

符號:class 人

位置: class Inheritance

6 個錯誤

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

      }

老師.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");
         }
        }
       }

歌手.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 中運行這個程序。 每次它都有效,但是當我從其他 package 導入類時,我收到上述錯誤。

您可以做的最好的事情是修復 Eclipse 中的設置,以便它工作。 一旦解決了這個問題,您就不必再擔心如何編譯了。

無論如何,要回答您提出的問題:

為了從命令行編譯您的文件,您需要位於java files目錄中。 這必須是您的工作目錄,因為它是包含最外層 package 的根目錄, oops

然后你需要先編譯person 您不能單獨編譯Inheritance 編譯器將只識別Singer中使用的類InheritanceTeacherperson在每個類都被編譯之后。 使用文件的相對路徑名:

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 $

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. 我還更正了右花括號的數量以及與編譯和運行問題無關的類似微小細節。

當您嘗試我的建議時,您出了什么問題,對不起,我無法猜測。

希望您正在通過 Apni Kaksha Java Tustorials,這里我已經解決了您的問題,下面是我的目錄結構

root
  |
  ->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

這將解決您的問題

編碼快樂!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM