java 我应该将公共接口放在单独的文件中吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2389983/
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
Should I put a public interface in a separate file?
提问by Roman
I have the following code:
我有以下代码:
import com.apple.dnssd.*;
public interface IServiceAnnouncer {
public void registerService();
public void unregisterService();
public boolean isRegistered();
}
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This code is saved in a file called "HelloWorld.java". The Java compiler complains about this code. It writes that the class IServiceAnnounceris public and it should be declared in a file called "IServiceAnnouncer.java".
此代码保存在名为“HelloWorld.java”的文件中。Java 编译器抱怨此代码。它写道该类IServiceAnnouncer是公共的,应该在名为“IServiceAnnouncer.java”的文件中声明。
I have several questions about this:
我对此有几个问题:
Why would the compiler say that
IServiceAnnounceris a class? It's an interface. Or interface is a partial case of a class?If I put the interface
IServiceAnnouncerin a separate file called "IServiceAnnouncer.java" (as the compiler wants), how then can I use it from the "HelloWorld.java"?What does
public interfacemean? What is the difference between a public interface and non-public one?
为什么编译器会说这
IServiceAnnouncer是一个类?这是一个接口。或者接口是类的部分情况?如果我将接口
IServiceAnnouncer放在一个名为“IServiceAnnouncer.java”的单独文件中(如编译器所希望的那样),那么我如何从“HelloWorld.java”中使用它?什么
public interface意思?公共接口和非公共接口有什么区别?
回答by Brian Agnew
You should put it in a separate file. That way it's easier to swap in a different implementation, or make the interfaces (the API of your system) available for others to code against without knowing the details of your implementation, or having to drag in related dependencies.
你应该把它放在一个单独的文件中。这样就可以更轻松地交换不同的实现,或者使其他人可以使用接口(您系统的 API)进行编码,而无需知道您的实现的详细信息,或者不必拖入相关的依赖项。
e.g. implementations of common Java APIs - e.g. servlets - will have an implementation coded against the package of interfaces provided by Sun (in this case javax.servlet)
例如,常见 Java API 的实现(例如 servlet)将具有针对 Sun 提供的接口包编码的实现(在本例中javax.servlet)
How can you use it from your implementation ? By importing it. This is unnecessary if it's in the same package and you're compiling all your interfaces/implementations at once.
你如何从你的实现中使用它?通过导入。如果它在同一个包中并且您一次编译所有接口/实现,则这是不必要的。
Note that an interface compiles down to a .class file in the same way as an implementation (what you define using class).
请注意,接口编译成 .class 文件的方式与实现(您使用 定义的内容class)相同。
回答by Chris K
You don't have a choice. All public classes/interfaces must be in files named ClassOrInterfaceName.java.
你别无选择。所有公共类/接口都必须在名为 ClassOrInterfaceName.java 的文件中。
回答by Sean Owen
These answers are dancing around the right one.
这些答案在正确的答案周围跳舞。
- Yes, you can declare multiple classes in one file in Java.
- You cannot declare more than one public class, because:
- A public class's name must match its containing file name; this is your compile error
- 是的,您可以在 Java 中的一个文件中声明多个类。
- 您不能声明多个公共类,因为:
- 公共类的名称必须与其包含的文件名相匹配;这是你的编译错误
It is very strange in Java to declare multiple classes in one file, even though you can. Don't do it.
在 Java 中在一个文件中声明多个类是很奇怪的,即使你可以。不要这样做。
You put IServiceAnnouncer in a separate file, and import the class name in HelloWorld.java. You merely compile them at the same time, passing both file names to javac. That all works.
您将 IServiceAnnouncer 放在一个单独的文件中,并在 HelloWorld.java 中导入类名。您只需同时编译它们,将两个文件名传递给 javac。这一切都有效。
A public interface, like anything else that's public in Java, is a type that's visible and usable from any other class. Without "public", its visibility is called package-private, which means only things in the same package can use it.
与 Java 中的任何其他公共接口一样,公共接口是一种可以从任何其他类中看到和使用的类型。如果没有“public”,它的可见性称为包私有,这意味着只有同一个包中的东西才能使用它。
回答by erickson
The compiler is using the term "class" a little loosely. A more general term might be "type". I'm guessing the compiler uses the term "class" because it produces ".class" files of the same format from every type declaration (class, interface, and enum).
编译器使用术语“类”有点松散。更通用的术语可能是“类型”。我猜编译器使用的术语“类”,因为它从每一个类型声明产生相同格式的“.class”文件(class,interface,和enum)。
An interface doesn't have to be public. If it is declared without an access modifier, it can be accessed only within its "package."
接口不一定是public. 如果它是在没有访问修饰符的情况下声明的,则只能在其“包”中访问。
A Java type can (should) be declared in a package. Packages are a collection of Java types that should be built and deployed together. By default, all types in a package are implicitly imported, so if your interface is in the same package, it will be available to the HelloWorldclass.
Java 类型可以(应该)在包中声明。包是应该一起构建和部署的 Java 类型的集合。默认情况下,包中的所有类型都是隐式导入的,因此如果您的接口在同一个包中,则HelloWorld该类将可以使用它。
When you don't declare a package, a type is in the default package. So even if you just put the IServiceAnnouncerinterface in a separate file, it will be available to HelloWorld. Just compile both files at the same time.
当你不声明一个包时,一个类型在默认包中。因此,即使您只是将IServiceAnnouncer界面放在单独的文件中,它也可用于HelloWorld. 只需同时编译这两个文件。
回答by Pindatjuh
The problem with more than one class-definition in one file is the following error, which may occur when using Versioning and multiple developers are working on the same project:
duplicate class: X class X {Thus, it's preferred to place public (and also non-public, btw) classes in separate files. This will reduce the chance of having these errors.
If it's in the same folder, just use the
IServiceAnnouncer, if it's in a different folder,importit.publicis visible from all packages. Nopublicmeans a kind ofprotected(while the keywordprotectedis prohibited, weird huh!): it's only visible from within the same package. If you're not sure what a package is, check out the Java Tutorial. http://java.sun.com/docs/books/tutorial/java/package/packages.html
一个文件中多个class-definition的问题是下面的错误,在使用Versioning和多个开发者在同一个项目上工作时可能会出现:
duplicate class: X class X {因此,最好将公共(以及非公共,顺便说一句)类放在单独的文件中。这将减少出现这些错误的机会。
如果它在同一个文件夹中,只需使用
IServiceAnnouncer,如果它在不同的文件夹中,import则使用它。public从所有包中都可以看到。不是public一种protected(虽然关键字protected被禁止,很奇怪吧!):它只能在同一个包中可见。如果您不确定包是什么,请查看 Java 教程。http://java.sun.com/docs/books/tutorial/java/package/packages.html
回答by pkananen
1) The compiler is just complaining that you have 2 public top level types defined. You can't do that in Java. You can nest the interface in your class if you'd like, or make it non-public (default visibility), but that's not too common in Java, and probably not too useful here.
1) 编译器只是抱怨您定义了 2 个公共顶级类型。你不能在 Java 中做到这一点。如果您愿意,您可以将接口嵌套在您的类中,或者将其设为非公开(默认可见性),但这在 Java 中不太常见,在这里可能也不太有用。
2) You'll either need to implement the interface, or have access to an object that does it implement it.
2) 您要么需要实现该接口,要么有权访问实现它的对象。
3) Interfaces can either be public, or default (package) visibility. Methods in an interface don't need the public modifier as they're public by default.
3) 接口可以是公共的,也可以是默认的(包)可见性。接口中的方法不需要公共修饰符,因为它们默认是公共的。

