找不到Java主类(无错误)

时间:2017-02-11 02:21:36

标签: java inheritance

这是我的源代码,我将Project命名为Animal,但是当我运行它时。找到我的MAIN CLASS时出错。请帮我。我还是初学者,我愿意学习更多东西。

package animal;

public class Animal {  
    public Animal() {
        System.out.println("A new animal has been created!");
    }

    public void sleep() { 
        System.out.println("An animal sleeps...");
    }

    public void eat() {
        System.out.println("An animal eats...");
    }
}

public class Bird extends Animal {
    public Bird() {
        super();
        System.out.println("A new bird has been created!");
    }

    @Override
    public void sleep() {
        System.out.println("A bird sleeps...");
    }

    @Override
    public void eat() {
        System.out.println("A bird eats...");
    }
}

public class Dog extends Animal {
    public Dog() {
        super();
        System.out.println("A new dog has been created!");
    }

    @Override
    public void sleep() {
        System.out.println("A dog sleeps...");
    }

    @Override
    public void eat() {
        System.out.println("A dog eats...");
    }
}

public class MainClass {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Bird bird = new Bird();
        Dog dog = new Dog();
        System.out.println();

        animal.sleep();
        animal.eat();
        bird.sleep();
        bird.eat();
        dog.sleep();
        dog.eat();
    }
}

请帮助我。

3 个答案:

答案 0 :(得分:0)

理想情况下,您应该在自己的文件中将Bird,Dog和Animal放在同一目录中。然后你再创建一个类,一个测试器类,它有一个main函数,因为它与Bird,Dog和Animal在同一个目录中,它可以访问它们,你可以做你的主类现在做的事情。如果您需要将所有内容都放在一个文件中,请将每个类标题设为class Bird {等。然后将该文件重命名为MainClass.java并运行。

答案 1 :(得分:0)

请参阅:Can a java file have more than one class?

简而言之:每个.java文件只能有一个公共顶级类。

所以,你应该编辑你的代码:

package Animal;

class Animal {
    public Animal() {
        System.out.println("A new animal has been created!");
    }

    public void sleep() { 
        System.out.println("An animal sleeps...");
    }

    public void eat() {
        System.out.println("An animal eats...");
    }
}

class Bird extends Animal {
    public Bird() {
        super();
        System.out.println("A new bird has been created!");
    }

依旧......

答案 2 :(得分:0)

在研究代码时,只在您声明的类修饰符(public)上找到错误。

<强>&#34;你可以在java源文件中只有一个公共类修饰符&#34;

我们可以在具有这些约束的单个源文件中声明一个类:

  1. 每个源文件只应包含一个公共类和名称 该公共类应该与源的名称类似 文件。
  2. 如果要在源文件中声明主方法,则为main 应该在公共课堂上。
  3. 如果源文件中没有公共类,则main方法可以 在任何类中,我们都可以给源文件命名。
  4. 所以.. 你的代码是......

       class Animal {  
            public Animal() {
        System.out.println("A new animal has been created!");
       }
       public void sleep() { 
        System.out.println("An animal sleeps...");
      }
      public void eat() {
        System.out.println("An animal eats..."); }
      }
      class Bird extends Animal {
       public Bird() {
        super();
        System.out.println("A new bird has been created!");
       }
       @Override
       public void sleep() {
        System.out.println("A bird sleeps...");
       }
        @Override
        public void eat() {
        System.out.println("A bird eats...");
        }
        }
        class Dog extends Animal {
         public Dog() {
        super();
        System.out.println("A new dog has been created!");
        }
         @Override
         public void sleep() {
        System.out.println("A dog sleeps...");
        }
         @Override
         public void eat() {
         System.out.println("A dog eats...");
           }
          }
         public class MainClass {
         public static void main(String[] args) {
          Animal animal = new Animal();
          Bird bird = new Bird();
          Dog dog = new Dog();
          System.out.println();
    
        animal.sleep();
        animal.eat();
        bird.sleep();
        bird.eat();
        dog.sleep();
        dog.eat();}
        }