为什么我得到“找不到符号”错误?

时间:2020-05-29 03:08:42

标签: java oop inheritance

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

符号:班级老师

位置:类继承

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

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

符号:班级老师

位置:类继承

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

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

符号:Singer类

位置:类继承

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

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

符号:Singer类

位置:类继承

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最新版本运行该程序。 每次都有效,但是当我从另一个包中导入类时,出现了上述错误。

2 个答案:

答案 0 :(得分:1)

您能做的最好的事情是在Eclipse中修复安装程序,以使其能够正常工作。解决该问题后,您无需担心如何编译。

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

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

然后,您需要首先编译person。您不能单独编译Inheritance。在编译每个InheritancepersonTeacherSinger中使用的类之后,编译器才会识别它们。使用文件的相对路径名:

javac oops/Inheritance/person.java

(在Windows上,使用反斜杠而不是斜杠)。在person之后,编译TeacherSinger(以任何顺序)。最后编译Inheritance

编辑:这在使用bash的BSD Unix上有效:

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

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

当您尝试我的建议时,您出了什么问题,对不起,我无法猜测。

答案 1 :(得分:1)

希望您正在学习Apni Kaksha Java教程,这里您的问题已解决,下面是我的目录结构

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

这将解决您的问题

快乐编码!