Java 错误:需要类、接口或枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21699574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:09:21  来源:igfitidea点击:

Java error: class, interface, or enum expected

javacompiler-errorsjavac

提问by Z9z9z9

I need to know the output of this code. But it's not working. Maybe the code is wrong. I'm still learning how to use Java, and I tried fixing this for hours but still no luck.

我需要知道这段代码的输出。但它不起作用。可能是代码不对。我仍在学习如何使用 Java,我尝试修复了几个小时,但仍然没有运气。

Here is the code:

这是代码:

public class A 
{ 
    public A() 
    {
        System.out.println ("A");
    }
}
public class B extends A 
{
    public B() 
    {
        System.out.println ("B");
    }
}
public class C extends B 
{ 
    public C() 
    {
        System.out.println ("C");
    }
}

public static void main(String args[]) {

    A a = new A();  
    B b = new B();  
    C c = new C();  
}

Can anyone tell me what is wrong or missing in the code?

谁能告诉我代码中有什么问题或遗漏了什么?

采纳答案by nablex

For example:

例如:

public class Example {

    public static void main(String...args) {
        new C();
    }

    public static class A {
        public A() {
            System.out.println("A");
        }
    }
    public static class B extends A {
        public B() {
            System.out.println("B");
        }
    }
    public static class C extends B {
        public C() {
            System.out.println("C");
        }
    }
}

Also note that this might not print what you would expect. It would actually print:

另请注意,这可能不会打印您所期望的内容。它实际上会打印:

A
B
C

Why? Constructors are always chained to the super class.

为什么?构造函数总是链接到超类。

回答by Not a bug

Put your main method in a class.

将您的主要方法放在一个类中。

Filename : DemoClass.java

class A 
{ 
    public A() 
    {
        System.out.println ("A");
    }
}
class B extends A 
{
    public B() 
    {
        System.out.println ("B");
    }
}
class C extends B 
{ 
    public C() 
    {
        System.out.println ("C");
    }
}

public class DemoClass {

   public static void main(String args[]) {

       A a = new A();  
       B b = new B();  
       C c = new C();  
   }
}

Another point here is, you can have only public class in a file, so your ABand Call class can't be publicin same java file.

这里的另一点是,一个文件中只能有 public 类,所以你ABC所有的类不能public在同一个 java 文件中。

Your java file name must be same as public class name. i.e. here DemoClassis public class so file name will be DemoClass.java

您的 java 文件名必须与公共类名相同。即这里DemoClass是公共类所以文件名将是DemoClass.java

Java doc for getting started : getting started with java

Java 入门文档:Java 入门

回答by Mr. Polywhirl

You can, but it is not recommended, nest your classes in a file. It is perfectly valid.

您可以(但不建议)将类嵌套在文件中。这是完全有效的。

Notice in the output below that each successive child calls its parent's default constructor (super()) implicitly.

请注意,在下面的输出中,每个连续的子级都super()隐式调用其父级的默认构造函数 ( )。

I recommend you create the files: A.java, B.java, C.java, and InheritenceTest.java.

我建议你创建的文件:A.javaB.javaC.java,和InheritenceTest.java

public class InheritenceTest {
    public class A {
        public A() {
            System.out.println("A");
        }
    }

    public class B extends A {
        public B() {
            System.out.println("B");
        }
    }

    public class C extends B {
        public C() {
            System.out.println("C");
        }
    }

    public static void main(String args[]) {
        InheritenceTest i = new InheritenceTest();
        A a = i.new A();
        B b = i.new B();
        C c = i.new C();
    }
}

Output:

输出:

A
A
B
A
B
C

回答by Kostas Chalkias

Warning: You shouldn't have more than 1 public classes in 1 java file, not recommended. However, it could still work if you didn't use the 'public' identifier (or by using static or inside another class). But for a starter, I would recommend you to have them all in separate files.

警告:不建议在 1 个 java 文件中包含超过 1 个公共类。但是,如果您不使用“公共”标识符(或使用静态或在另一个类中),它仍然可以工作。但是对于初学者,我建议您将它们全部放在单独的文件中。

Error: Your main method does not belong to any class. I propose you create another class that includes the public static void main method to test your application.

错误:您的 main 方法不属于任何类。我建议您创建另一个包含 public static void main 方法的类来测试您的应用程序。

Info: keep a look at inheritance as your printings might not be what you expect. (Constructor of class B calls the constructor of A, and constructor of class C calls the constructor B which in turn calls the constructor of A).

信息:请注意继承,因为您的打印可能不是您所期望的。(B类的构造函数调用A的构造函数,C类的构造函数调用构造函数B,B又调用A的构造函数)。

That's why you get

这就是为什么你得到

A
A  
B
A
B
C

*due to A() it prints A, then due to B() it prints A B and finally due to C() it prints A B C.

In your case, I would try the following:

在您的情况下,我会尝试以下操作:

//Filename: A.java
public class A { 
    public A() {
        System.out.println ("A");
    }
}


//Filename: B.java
public class B extends A { 
    public B() {
        System.out.println ("B");
    }
}


//Filename: C.java
public class C extends B { 
    public C() {
        System.out.println ("C");
    }
}


//Filename: Test.java
//use a Test class for testing
public class Test {
   public static void main(String args[]) {
       A a = new A();  
       B b = new B();  
       C c = new C();  
   }
}