没看懂的用途!if (!origin) 中的运算符。作者解释说它正在测试实例变量 origin 以查看它的值是否非零,但我不太明白这是什么意思。
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
@property int x, y;
@end
#import "XYpoint.h"
@implementation XYPoint
@synthesize x, y;
@end
#import <Foundation/Foundation.h>
@class XYPoint;
@interface Rectangle: NSObject
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
@end
#import "Rectangle.h"
#import "XYpoint.h"
@implementation Rectangle {
XYPoint *origin;
}
-(void) setOrigin:(XYPoint *)pt {
if (! origin)
origin = [[XYpoint alloc] init];
origin.x = pt.x;
origin.y = pt.y
}
-(XYPoint *) origin {
return origin;
}
@end