在 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
这样,如果我包含 .h 文件,我将可以访问文件中定义的协议。我正在Java中寻找类似的结构,因为我发现它在某些我想避免创建太多文件(接口文件+类文件)的情况下很有用。这样我就可以声明:
public class MyImplementation implements AnotherClass.MyInterface{
AnotherClass otherClass;
}
我认为接口内的嵌套类是要走的路。我是对的吗?或者Java中没有类似的东西?