简体   繁体   English

从包含多个类的 java 文件导入 java class

[英]import a java class from a java file which contains multiple classes

i am going through a situation where i have created multiple classes for custom exception in a single java file.我正在经历一种情况,我在单个 java 文件中为自定义异常创建了多个类。 as follow如下

    public class IllegalArgumentException extends Throwable {
        public IllegalArgumentException(String exceptionString) {
            super(exceptionString);
        }
    }

    class InvalidDirectoryException extends Throwable {
        public InvalidDirectoryException(String exceptionString) {
            super(exceptionString);
        }
    }

    class InvalidJsonFormatException extends Throwable {
        public InvalidJsonFormatException(String exceptionString) {
            super(exceptionString);
        }
    }

    class InvalidFileTypeException extends Throwable {
        public InvalidFileTypeException(String exceptionString) {
            super(exceptionString);
        }
    }

when I try to import one of the class then it is not resolving.当我尝试导入 class 之一时,它没有解决。 I am only able to import IllegalArgumentException class.我只能导入 IllegalArgumentException class。 I dont want to make multiple java file for each class.我不想为每个 class 制作多个 java 文件。 So is there way to do it?那么有没有办法做到呢?

I dont want to make multiple java file for each class.我不想为每个 class 制作多个 java 文件。

You have to if you want to access them from other packages.如果您想从其他软件包访问它们,则必须这样做。 In Java, every file must have exactly one public class.在 Java 中,每个文件必须只有一个公共 class。 That is a rule.这是一条规则。 There could be valid reasons why you don't want a separate file for each class, but Java doesn't care about that.您不希望为每个 class 使用单独的文件可能有正当的理由,但 Java 并不关心这一点。

You could try making each of them static inner classes in a single outer class, and then using import static , but that is a bad practice, as you would be abusing the intended purpose of inner classes, so I don't encourage using that method.您可以尝试在单个外部 class 中创建每个 static 内部类,然后使用import static ,但这是一种不好的做法,因为我会鼓励使用内部类的目的,所以我会滥用. But you could.但你可以。

Create one general public class and put all other classes as inner classes inside the main public class.创建一个通用公共 class 并将所有其他类作为内部类放入主公共 class 中。

Example:例子:

public class AllExeptions{

   class IllegalArgumentException extends Throwable {
        public IllegalArgumentException(String exceptionString) {
            super(exceptionString);
        }
    }

    class InvalidDirectoryException extends Throwable {
        public InvalidDirectoryException(String exceptionString) {
            super(exceptionString);
        }
    }

    class InvalidJsonFormatException extends Throwable {
        public InvalidJsonFormatException(String exceptionString) {
            super(exceptionString);
        }
    }

    class InvalidFileTypeException extends Throwable {
        public InvalidFileTypeException(String exceptionString) {
            super(exceptionString);
        }
    }
}

If you are not familiar with the Java inner class, then I recommend you this nince inner class tutorial by TutorialsPoint: https://www.tutorialspoint.com/java/java_innerclasses.htm If you are not familiar with the Java inner class, then I recommend you this nince inner class tutorial by TutorialsPoint: https://www.tutorialspoint.com/java/java_innerclasses.htm

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

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