Java 错误 - 预期的类接口或枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19153816/
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-12 14:33:52  来源:igfitidea点击:

Error - Class interface or enum expected

javaenumspackagesclass-structure

提问by sugam

I am new to java and i am writing this code in notepad which is giving me errors.In netbeans although package is already defined.How to do this in notepad?

我是 Java 新手,我正在记事本中编写这段代码,这给了我错误。在 netbeans 中,虽然已经定义了包。如何在记事本中执行此操作?

package A;
class A {
    private String name;
    protected String company="oracle";  
    A(String name) {
        this.name = name;
        System.out.println(name);
    }
}

public class B extends A {
    // A public class constant
    public final static String st = "Public access modifiers";

    B(String name) {
        super(name);
    }
    void getCompany()
    {
        System.out.println(company);
    }

}

package B;//getting class interface or enum expected 
public class Application {
    public static void main(String[] args) {
        System.out.println(st);
        B b=new B("Java");
        b.getCompany();
    }
}

采纳答案by ppeterka

You can not put different packages into the same source file... You have to create the appropriate folder structure, and separate Java source files for the sources in each package...

您不能将不同的包放入同一个源文件中...您必须创建适当的文件夹结构,并为每个包中的源单独的 Java 源文件...

Also, to be able to reference classes from other packages, you have to importthem appropriately, and make sure they are actually on the classpathboth for compiling and running too......

此外,为了能够从其他包中引用类,您必须import适当地引用它们,并确保它们实际上位于类路径上以进行编译和运行......

Recommended reading

推荐阅读

回答by SpringLearner

package B;//getting class interface or enum expected 

remove this line Packagedeclaration should be the first lineof the source file.

删除这一行 Package声明应该是源文件的第一行

You can not write 2 or more different packageswith in the same source

您不能在同一个源中编写2 个或更多不同的包

回答by Suresh Atta

The PackageOrTypeName must be the canonical name (§6.7) of a package, a class type, an interface type, an enum type, or an annotation type.

PackageOrTypeName 必须是包、类类型、接口类型、枚举类型或注释类型的规范名称(第 6.7 节)。

That is what it is telling, and remove multiple declarations of package

这就是它所说的,并删除多个声明 package

And you should import the class B, When they both belongs to different packages.

并且您应该导入 class B,当它们都属于不同的包时。

import packagePath.B;

If a single-type-import declaration imports a type whose simple name is n, and the compilation unit also declares a top level type (§7.6) whose simple name is n, a compile-time error occurs.

如果单类型导入声明导入了一个简单名称为 n 的类型,并且编译单元还声明了一个简单名称为 n 的顶级类型(第 7.6 节),则会发生编译时错误。

Language Specification

语言规范

Side note:Do not write multiple classes in a single java file. Later It's very hard to maintain the code.

旁注:不要在单个 java 文件中编写多个类。后来代码维护起来很吃力。

回答by sara

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

package 语句应该是源文件中的第一行。每个源文件中只能有一个 package 语句,它适用于文件中的所有类型。