我应该将公共接口放在单独的文件中吗?
我有以下代码:
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!");
}
}
该代码保存在名为“HelloWorld.java”的文件中。 Java 编译器抱怨这段代码。它写道,IServiceAnnouncer
类是公共的,应该在名为“IServiceAnnouncer.java”的文件中声明它。
我对此有几个问题:
为什么编译器会说
IServiceAnnouncer
是一个类?这是一个接口。或者接口是类的部分情况?如果我将接口
IServiceAnnouncer
放在一个名为“IServiceAnnouncer.java”的单独文件中(如编译器所希望的那样),那么我如何从“HelloWorld.java”中使用它?公共接口
是什么意思?公共接口和非公共接口有什么区别?
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 IServiceAnnouncer
is public and it should be declared in a file called "IServiceAnnouncer.java".
I have several questions about this:
Why would the compiler say that
IServiceAnnouncer
is a class? It's an interface. Or interface is a partial case of a class?If I put the interface
IServiceAnnouncer
in a separate file called "IServiceAnnouncer.java" (as the compiler wants), how then can I use it from the "HelloWorld.java"?What does
public interface
mean? What is the difference between a public interface and non-public one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该将其放在单独的文件中。这样,就可以更轻松地交换不同的实现,或者使接口(系统的 API)可供其他人进行编码,而无需了解实现的详细信息,或者不必拖入相关的依赖项。
例如,常见 Java API 的实现(例如 servlet)将具有针对 Sun 提供的接口包编码的实现(在本例中为
javax.servlet
)您如何在实现中使用它?通过导入它。如果它位于同一个包中并且您要立即编译所有接口/实现,则这是不必要的。
请注意,接口以与实现(使用
class
定义的内容)相同的方式编译为 .class 文件。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.
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
)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
).这些答案都围绕着正确的答案跳舞。
在Java中在一个文件中声明多个类是非常奇怪的,尽管你可以。不要这样做。
您将 IServiceAnnouncer 放在单独的文件中,并在 HelloWorld.java 中导入类名。您只需同时编译它们,将两个文件名传递给 javac 即可。这一切都有效。
公共接口与 Java 中的其他公共接口一样,是一种对任何其他类都可见且可用的类型。如果没有“public”,它的可见性被称为package-private,这意味着只有同一包中的东西才能使用它。
These answers are dancing around the right one.
It is very strange in Java to declare multiple classes in one file, even though you can. Don't do it.
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.
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.
你别无选择。所有公共类/接口必须位于名为 ClassOrInterfaceName.java 的文件中。
You don't have a choice. All public classes/interfaces must be in files named ClassOrInterfaceName.java.
编译器使用术语“类”有点宽松。更通用的术语可能是“类型”。我猜测编译器使用术语“class”,因为它从每个类型声明(
class
、interface
和生成相同格式的“.class”文件>枚举
)。接口不必是
公共
。如果它的声明没有访问修饰符,则只能在其“包”内访问它。Java 类型可以(应该)在包中声明。包是应该一起构建和部署的 Java 类型的集合。默认情况下,包中的所有类型都是隐式导入的,因此如果您的接口位于同一个包中,它将可供
HelloWorld
类使用。当您不声明包时,类型位于默认包中。因此,即使您只是将
IServiceAnnouncer
接口放在单独的文件中,它也可供HelloWorld
使用。只需同时编译这两个文件即可。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
, andenum
).An interface doesn't have to be
public
. If it is declared without an access modifier, it can be accessed only within its "package."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
HelloWorld
class.When you don't declare a package, a type is in the default package. So even if you just put the
IServiceAnnouncer
interface in a separate file, it will be available toHelloWorld
. Just compile both files at the same time.一个文件中存在多个类定义的问题是以下错误,当使用版本控制并且多个开发人员正在处理同一项目时可能会出现此错误:
因此,最好将公共(以及非公共,顺便说一句)类放在单独的文件中。这将减少出现这些错误的机会。
如果它在同一个文件夹中,只需使用
IServiceAnnouncer
,如果它在不同的文件夹中,导入
它。public
在所有包中都是可见的。没有public
意味着一种protected
(虽然关键字protected
是被禁止的,奇怪吧!):它只能在同一个包中可见。如果您不确定包是什么,请查看 Java 教程。 http://java.sun.com/docs/books/ tutorial/java/package/packages.htmlThe 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:
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,import
it.public
is visible from all packages. Nopublic
means a kind ofprotected
(while the keywordprotected
is 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.html1) 编译器只是抱怨你定义了 2 个公共顶级类型。在 Java 中你不能这样做。如果您愿意,可以将接口嵌套在类中,或者将其设置为非公共(默认可见性),但这在 Java 中不太常见,并且在这里可能不太有用。
2)您要么需要实现该接口,要么有权访问实现该接口的对象。
3) 接口可以是公共的,也可以是默认(包)可见性的。接口中的方法不需要 public 修饰符,因为它们默认是公共的。
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.
2) You'll either need to implement the interface, or have access to an object that does it implement it.
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.