Java 什么是辅助类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22521288/
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
What are auxiliary classes?
提问by BrainStorm.exe
I know these questions may sound stupid, but in Java, what are Auxiliary classes, how does some one write one, and how does the compiler know that something is an Auxiliary class?
我知道这些问题可能听起来很愚蠢,但是在 Java 中,什么是辅助类,某人如何编写一个,以及编译器如何知道某个东西是辅助类?
Edit: The reason I ask this is because the compiler is generating a warning regarding an object in an external library, and I want to know why.
编辑:我问这个的原因是因为编译器正在生成关于外部库中的对象的警告,我想知道为什么。
Edit 2:
编辑2:
Here is the compiler warning for those who want it:
这是那些想要它的人的编译器警告:
warning: auxiliary class Pattern in jregex/Pattern.java should not be accessed from outside its own source file
采纳答案by Artem
As descried in Java specification here, you can specify more than one class in one .java file. The class which name matches .java file name will be the main class which can be declared public and be visible to other classes. All other classes in the file therefore are "auxilary" classes. Auxilary class can NOT be declared public and (as @trashgod rightfully pointed out) therefore they only be declared with package-private access. For instance for AClass.java file:
正如此处的Java 规范中所述,您可以在一个 .java 文件中指定多个类。名称与 .java 文件名匹配的类将是主类,它可以被声明为 public 并且对其他类可见。因此,文件中的所有其他类都是“辅助”类。辅助类不能被声明为 public 并且(正如@trashgod 正确指出的那样)因此它们只能通过包私有访问来声明。例如对于 AClass.java 文件:
public class AClass {
private AuxilaryClass a;
}
class AuxilaryClass {
private int b;
}
AuxilaryClass class can't be public and is not visible outside this AClass.java file.
AuxilaryClass 类不能是公共的,并且在这个 AClass.java 文件之外是不可见的。
However, using auxilary classes considered extremely bad style and against Java Code Convention. Please use separate or innerclasses if really needed.
但是,使用辅助类被认为是非常糟糕的风格并且违反了Java 代码约定。如果确实需要,请使用单独的或内部的类。
Edit: The term "Auxilary" is not Oracle/Sun official terminology. It has been introduced (or used) here: http://www.youtube.com/watch?v=miTM9rY3He0and/or here: http://doc.sumy.ua/prog/java/langref/ch05_03.htm
编辑:术语“辅助”不是 Oracle/Sun 官方术语。它已在此处引入(或使用):http: //www.youtube.com/watch?v=miTM9rY3He0和/或此处:http: //doc.sumy.ua/prog/java/langref/ch05_03.htm
回答by amalloy
An auxiliary class isn't any kind of official or technical thing as far as I know. Someone might describe a class as auxiliary if it were addressing a secondary concern, or something, but the compiler doesn't have any idea what an auxiliary class is, and neither do I.
据我所知,辅助课程不是任何官方或技术性的东西。如果某个类解决次要问题或其他问题,有人可能会将其描述为辅助类,但编译器不知道辅助类是什么,我也不知道。
In general, if you have error messages from the computer, please paste them in their entirety. If you think the compiler is upset about an auxiliary class, paste the error message: someone else will be able to make sense of it, whereas currently it's being filtered through some kind of confusion that's made you think auxiliary classes are a real thing!
通常,如果您有来自计算机的错误消息,请将其全部粘贴。如果您认为编译器对辅助类感到不安,请粘贴错误消息:其他人将能够理解它,而目前它正在通过某种混淆进行过滤,使您认为辅助类是真实的!