我正在編寫一個程序,它有一個SecretNumber這個類持有一個數字並且有一個方法返回猜測是否太低,太正確或者正確。
該方法返回三個對象之一的對象,這樣的定義:使用空類作爲數據類型
@interface TooHigh : NSObject
@end
@implementation TooHigh
@end
@interface TooLow : NSObject
@end
@implementation TooLow
@end
@interface JustRight : NSObject
@end
@implementation JustRight
@end
的SecretNumber推測法,然後像這樣實現的:
@implementation SecretNumber
{
int Secret;
}
-(id) guessSecret:(int)g
{
if (g>Secret) {
return [[TooHigh alloc] init];
} else {
if (g<Secret) {
return [[TooLow alloc] init];
} else {
return [[JustRight alloc] init];
}
}
}
所以我的問題存在這被認爲是好的編程形式,還是我最好送回1,2,3或3個隨機字符(一個意思是太高,另一個太低等)?
偉大的總結!我將在每個類中封裝每個返回值的行爲,這樣我可以稍後添加更多的功能。 – TheInnerParty 2015-04-02 18:58:02