在Java中,您可以在单个文件中定义多个顶级类,只要其中一个是公共的(参见JLS§7.6).见下面的例子.
是否有此技术整洁名(类似于inner,nested,anonymous)?
JLS表示系统可能会强制执行这些辅助类不能的限制referred to by code in other compilation units of the package,例如,它们不能被视为包私有.这真的在Java实现之间发生了变化吗?
例如,PublicClass.java:
package com.example.multiple;
public class PublicClass {
PrivateImpl impl = new PrivateImpl();
}
class PrivateImpl {
int implementationData;
}
Run Code Online (Sandbox Code Playgroud) 在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
Run Code Online (Sandbox Code Playgroud)
这样,如果我包含.h文件,我将可以访问文件中定义的协议.我在Java中寻找类似的结构,因为我觉得它在某些情况下很有用,我想避免创建太多的文件(接口文件+类文件).那样我可以声明:
public class MyImplementation implements AnotherClass.MyInterface{
AnotherClass otherClass;
}
Run Code Online (Sandbox Code Playgroud)
我认为接口内的嵌套类是要走的路.我是对的?或者Java中没有类似的东西?