Java 中找不到符号错误

发布于 2024-12-20 13:40:20 字数 1046 浏览 1 评论 0原文

我有 3 个类文件,当我编译扩展另一个类的类时,会出现编译错误,例如找不到符号。

public class  Animal {  
    public static void hide() {  
        System.out.println("Hide Method Of Animal");  
    }  

    public void override() {  
        System.out.println("The Override method of Animal");  
    }  
}  

public class Cat extends Animal {  
    public static void hide() {  
        System.out.println("Hide Method Of Cat");  
    }  

    public void override() {  
        System.out.println("The Override method of Animal");  
    }  
}  

public class TestAnimal {  
    public static void main(String args[]) {  
        Cat myCat = new Cat();  
        Animal myAnimal =  (Animal)myCat;  
        myAnimal.hide();  
        myAnimal.override();  
    }  
}  

我收到此错误:

TestAnimal.java:6: cannot find symbol  
symbol  : class Cat  
location: class com.Test.TestAnimal  
            Cat myCat = new Cat();  
            ^  

对此有任何帮助吗???

I have 3 class file, when i compile the class which extends the other class gives compilation error like symbol not found.

public class  Animal {  
    public static void hide() {  
        System.out.println("Hide Method Of Animal");  
    }  

    public void override() {  
        System.out.println("The Override method of Animal");  
    }  
}  

public class Cat extends Animal {  
    public static void hide() {  
        System.out.println("Hide Method Of Cat");  
    }  

    public void override() {  
        System.out.println("The Override method of Animal");  
    }  
}  

public class TestAnimal {  
    public static void main(String args[]) {  
        Cat myCat = new Cat();  
        Animal myAnimal =  (Animal)myCat;  
        myAnimal.hide();  
        myAnimal.override();  
    }  
}  

I get this error:

TestAnimal.java:6: cannot find symbol  
symbol  : class Cat  
location: class com.Test.TestAnimal  
            Cat myCat = new Cat();  
            ^  

Any help on this???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单爱 2024-12-27 13:40:20

是的,编译后看看每个.java文件是否都有.class文件。如果没有,请确保每个文件都有一个 .class 文件。

运行时,使用 -classpath 参数指向所有 .class 文件所在的目录。

听起来您可以使用一些有关编译和运行 Java 的说明。

http://www.horstmann.com/bigj/help/compiler/tutorial.html

这是勺子喂养的答案:

  1. 用命令转到你的 d:\Mine\CoreJava\Programming 目录
    壳。
  2. 创建目录 /classes
  3. 通过键入 javac -d classes -cp classes src/*.java 进行编译
  4. 您应该会立即看到创建的所有 .class 文件。
  5. 通过输入 java -cp .;classes FullClassNameOfYourMain 来运行

Yes, see if there is a .class file for each .java file after you compile. If not, make sure that each one has a .class file.

When you run, use the -classpath argument to point to the directory where all the .class files live.

You sound like you could use some instruction on compiling and running Java.

http://www.horstmann.com/bigj/help/compiler/tutorial.html

Here's the spoon feeding answer:

  1. Go to your d:\Mine\CoreJava\Programming directory in a command
    shell.
  2. Create a directory /classes
  3. Compile by typing javac -d classes -cp classes src/*.java
  4. You should see all the .class files created at once.
  5. Run by typing java -cp .;classes FullClassNameOfYourMain
终止放荡 2024-12-27 13:40:20

你的代码绝对没有问题。这就是我所做的。看看你是否做对了一切:

$ cat > Animal.java
public class  Animal
{
    public static void hide()
    {
    System.out.println("Hide Method Of Animal");
    }

    public void override()
    {
    System.out.println("The Override method of Animal");
    }
}

$ cat > Cat.java
public class Cat extends Animal
{
    public static void hide()
    {
    System.out.println("Hide Method Of Cat");
    }

    public void override()
    {
    System.out.println("The Override method of Animal");
    }
}

$ cat > TestAnimal.java
public class TestAnimal
{
    public static void main(String args[])
    {
    Cat myCat = new Cat();
    Animal myAnimal =  (Animal)myCat;
    myAnimal.hide();
    myAnimal.override();
    }
}

$ javac TestAnimal.java

$ ls
Animal.class  Animal.java  Cat.class  Cat.java  TestAnimal.class  TestAnimal.java

$ java TestAnimal
Hide Method Of Animal
The Override method of Animal

我使用复制粘贴来获取你的代码,如你所见。请注意,编译后,除了 .java 文件之外,还会出现三个 .class 文件。

There's absolutely nothing wrong with your code. Here's what I've done. See if you did everything right:

$ cat > Animal.java
public class  Animal
{
    public static void hide()
    {
    System.out.println("Hide Method Of Animal");
    }

    public void override()
    {
    System.out.println("The Override method of Animal");
    }
}

$ cat > Cat.java
public class Cat extends Animal
{
    public static void hide()
    {
    System.out.println("Hide Method Of Cat");
    }

    public void override()
    {
    System.out.println("The Override method of Animal");
    }
}

$ cat > TestAnimal.java
public class TestAnimal
{
    public static void main(String args[])
    {
    Cat myCat = new Cat();
    Animal myAnimal =  (Animal)myCat;
    myAnimal.hide();
    myAnimal.override();
    }
}

$ javac TestAnimal.java

$ ls
Animal.class  Animal.java  Cat.class  Cat.java  TestAnimal.class  TestAnimal.java

$ java TestAnimal
Hide Method Of Animal
The Override method of Animal

I used copy-paste to get your code, as you can see. Notice that after compiling there are three .class files in addition to your .java files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文