在 Java 中的类文件中声明接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11850994/
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
Declaring Interface inside class file in Java
提问by Pablo
in Objective-C we can define a protocol and a implementation in the same header file. For example:
在 Objective-C 中,我们可以在同一个头文件中定义协议和实现。例如:
@class GamePickerViewController;
@protocol GamePickerViewControllerDelegate <NSObject>
- (void)gamePickerViewController:
(GamePickerViewController *)controller
didSelectGame:(NSString *)game;
@end
@interface GamePickerViewController : UITableViewController
@property (nonatomic, weak) id <GamePickerViewControllerDelegate> delegate;
@property (nonatomic, strong) NSString *game;
@end
This way if I include the .h file I will have access to the protocol defined inside the file. I'm looking for a similar structure in Java cause I find it useful in some cases where I would like to avoid creating too many files (interface file+class file). That way I could declare:
这样,如果我包含 .h 文件,我将可以访问文件中定义的协议。我正在 Java 中寻找类似的结构,因为我发现它在某些情况下很有用,我希望避免创建太多文件(接口文件 + 类文件)。这样我就可以声明:
public class MyImplementation implements AnotherClass.MyInterface{
AnotherClass otherClass;
}
I Think nested classes inside interfaces is the way to go. I am correct? or there's nothing similar in Java?
我认为接口内的嵌套类是要走的路。我是对的?或者Java中没有类似的东西?
回答by James
You can nest classes, and have the nested class be public static, this allows them to be in the same Source file (although it is unusual, it is more normal to put them together in a package and use seperate source files)
您可以嵌套类,并将嵌套类设置为 public static,这允许它们在同一个 Source 文件中(虽然不常见,但将它们放在一个包中并使用单独的源文件更正常)
For example this is allowed
例如这是允许的
public class AnotherClass {
public static interface MyInterface{
// Interface code
}
public static class MyClass{
//class code
}
}
And in another file
在另一个文件中
public class MyImplementation implements AnotherClass.MyInterface{
}
Another option would be
另一种选择是
public interface MyInterface{
public static class MyClass implements MyInterface{
}
}
and then access the class with MyInterface.MyClass (see java.awt.geom.Point
for an example of this sort of structure)
然后使用 MyInterface.MyClass 访问该类(参见java.awt.geom.Point
此类结构的示例)
回答by Alex Coleman
You can nest classes and interfaces like that, and have them be public! However, you can't implement/extend a class/interface where the class extended is nested in the class you want to extend it
您可以像这样嵌套类和接口,并将它们设为公开!但是,您不能实现/扩展类/接口,其中扩展的类嵌套在要扩展的类中
So this won't work:
所以这行不通:
class A extends A.B {
public class B {
}
}
It's fine having class B public in there, but the top level class cannot extend an internal class.
可以在那里公开 B 类,但顶级类不能扩展内部类。
回答by pb2q
Using nested classes you can achieve something similar: packaging an implementation along with an interface, e.g.:
使用嵌套类,您可以实现类似的功能:将实现与接口一起打包,例如:
public interface MyInterface
{
public class Implementation implements MyInterface
{
}
}
Now you have both MyInterface
and a concrete implementation MyInterface.Implementation
.
现在您已经有了MyInterface
一个具体的实现MyInterface.Implementation
。
回答by sahil
interface B {
public void show();
class b implements B {
public void show() {
System.out.println("hello");
}
}
}
class A extends B.b {
public static void main(String ar[]) {
B.b ob=new B.b();
ob.show();
}
}
回答by Sled
What you could do is define the interface and then have a default implementation as an anonymous inner class, class static variable.
您可以做的是定义接口,然后将默认实现作为匿名内部类,类静态变量。
interface AProtocol {
String foo();
static final AProtocol DEFAULT_IMPLEMENTATION = new AProtocol(){
@Override
public String foo(){
return "bar!";
}
};
}
Is that what you mean?
你是这个意思吗?
回答by Code-Apprentice
The Java API does this kind of thing quite often with classes. For example JFormattedTextFiled.AbstractFormatter. Notice that the declaration includes the static
modifier. I don't see why you couldn't do this with interfaces as well.
Java API 经常用类来做这种事情。例如JFormattedTextFiled.AbstractFormatter。请注意,声明包含static
修饰符。我不明白为什么你不能用接口来做到这一点。