in Objective-C we can define a protocol and a implementation in the same header file.
For example:
@class GamePickerViewController;
@protocol GamePickerViewControllerDelegate
- (void)gamePickerViewController:
(GamePickerViewController *)controller
didSelectGame:(NSString *)game;
@end
@interface GamePickerViewController : UITableViewController
@property (nonatomic, weak) id 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:
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?
解决方案
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)
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)