简体   繁体   English

Notepad ++中的多类

[英]Multi class in Notepad++

So I am unfamiliar with Notepad++ and have a working program in Blue J. I tried to transfer it over but I continue to receive the error: 所以我不熟悉Notepad ++,并且在Blue J中有一个正常工作的程序。我试图将其转移过来,但我仍然收到错误消息:

"Average.java:5: error: class UserInput is public, should be declared in a file named UserInput.java public class UserInput ^ “ Average.java:5:错误:UserInput类是公共的,应该在名为UserInput.java的文件中声明public class UserInput ^

Note: Average.java uses unchecked or unsafe operations. 注意:Average.java使用未经检查或不安全的操作。

Note: Recompile with -Xlint:unchecked for details. 注意:使用-Xlint:unchecked重新编译以获取详细信息。 1 error" 1个错误”

I think it has to do with how I wrote each class in but I am unsure how to fix it. 我认为这与我编写每个类的方式有关,但是我不确定如何解决。

在此处输入图片说明

Only one public class is allowed in a file, and it should have the same name as the file. 文件中仅允许一个公共类,并且该公共类应与文件同名。

The first solution is quite well define in the error. 第一个解决方案很好地定义了错误。 " UserInput [...] should be declared in a file named UserInput.java ". 应该在名为UserInput.java的文件中声明UserInput [...]
The second solution is to change the visibility of the class 第二种解决方案是更改类的可见性

in A.java A.java中

public class A {}
class B{}
protected class C{}
private class D{}

public class E{} //THIS IS NOT ALLOWED, it should be in E.java

Note that even if only one public class can be define in a file, it is not mandatory to have a public class. 请注意,即使在一个文件中只能定义一个公共类,也不一定要有一个公共类。

X.java X.java

protected class X{} //this is valid. 

But don't add another class in this file as public, only X can be public in X.java 但是不要在此文件中添加其他类作为public,在X.java只有X可以是public

In Java there can only be 1 public class declared per java file. 在Java中,每个Java文件只能声明1个公共类。 So to quickly resolve your issue you could simply split out the UserInput class into it's own file called UserInput.java , it is as simple as that. 因此,要快速解决您的问题,您可以将UserInput类拆分成自己的文件UserInput.java ,就这么简单。

For a bit complexity though you could look into Inner Classes or Local Classes which would allow you declare an additional class(es) within the one file. 虽然有点复杂,但是您可以查看Inner ClassesLocal Classes ,这将使您可以在一个文件中声明一个或多个其他类。

Have a read of 阅读

Here is an example of a LocalClass (Code taken from Local Inner Classes Documentation ) 这是LocalClass的示例(代码取自Local内部类文档

public class LocalClassExample {

    static String regularExpression = "[^0-9]";

    public static void validatePhoneNumber(String phoneNumber1, String phoneNumber2) {
        final int numberLength = 10;

        // Valid in JDK 8 and later:

        // int numberLength = 10;

        class PhoneNumber {

            String formattedPhoneNumber = null;

            PhoneNumber(String phoneNumber) {
                // numberLength = 7;
                String currentNumber = phoneNumber.replaceAll(
                regularExpression, "");
                if (currentNumber.length() == numberLength)
                    formattedPhoneNumber = currentNumber;
                else
                    formattedPhoneNumber = null;
            }

            public String getNumber() {
                return formattedPhoneNumber;
            }

            // Valid in JDK 8 and later:

            // public void printOriginalNumbers() {
                // System.out.println("Original numbers are " + phoneNumber1 +
               // " and " + phoneNumber2);
            // }
        }

    PhoneNumber myNumber1 = new PhoneNumber(phoneNumber1);
    PhoneNumber myNumber2 = new PhoneNumber(phoneNumber2);

    // Valid in JDK 8 and later:

    // myNumber1.printOriginalNumbers();

    if (myNumber1.getNumber() == null) 
        System.out.println("First number is invalid");
    else
        System.out.println("First number is " + myNumber1.getNumber());
    if (myNumber2.getNumber() == null)
        System.out.println("Second number is invalid");
    else
        System.out.println("Second number is " + myNumber2.getNumber());

    }

    public static void main(String... args) {
       validatePhoneNumber("123-456-7890", "456-7890");
    }
}

This code would print out 此代码将打印出来

First number is 1234567890
Second number is invalid

As @AxelH has stated this could be a bit complex for someone without much knowledge, however this is a fun little exercise to play with. 正如@AxelH所说的那样,对于一个没有太多知识的人来说,这可能有点复杂,但这是一个有趣的小练习。 For a more simple I would refer you to @AxelH answer. 为了更简单,我将向您推荐@AxelH答案。

class UserInput is public, should be declared in a file named UserInput.java UserInput类是公共的,应在名为UserInput.java的文件中声明

I guess the exception is self-explanatory. 我想例外是不言而喻的。 Just rename to File name to UserInput.java and it will work! 只需将File name重命名为UserInput.java即可使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM