关于myrect.origin=mypoint中调用setOrigin的问题

来源:互联网 发布:mac win7 编辑:程序博客网 时间:2024/05/21 21:34
代码如下:
1-定义XYPoint接口文件#import <Foundation/Foundation.h>
@interface XYpoint : NSObject
@property int x,y;
-(void) setX:(int)xVal andY:(int) yVal;
@end

2-XYpoint实现代码
#import "XYpoint.h"
@implementation XYpoint
@synthesize x,y;
-(void) setX:(int)xVal andY:(int)yVal
{
    x = xVal;
    y = yVal;
}
@end

3-rectangl类接口代码
#import <Foundation/Foundation.h>
@class XYpoint;
@interface Rectangl : NSObject
@property int width,height;
 
-(XYpoint *) origin;
-(void) setOrigin:(XYpoint *) pt;
-(void) setWidth:(int)w andHeight:(int)y;
-(int) area;
-(int) perimeter;
@end

4-rectangl实现代码
#import "Rectangl.h"
#import "XYpoint.h"
@implementation Rectangl
{
    XYpoint *origin;
}
@synthesize width,height;
-(void) setWidth:(int)w andHeight:(int)y
{
    width = w;
    height = y;
}
-(void) setOrigin:(XYpoint *)pt
{
    origin = pt;
}
-(int) area
{
    return width*height;
}
-(int) perimeter
{
    return (width+height)*2;
}
-(XYpoint *) origin
{
    return origin;
}
@end


5-主函数
#import <Foundation/Foundation.h>
#import "XYpoint.h"
#import "Rectangl.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Rectangl *myrect = [[Rectangl alloc]init];
        XYpoint *myPoint = [[XYpoint alloc]init];
         
        [myPoint setX:100 andY:200];
        [myrect setWidth:5 andHeight:8];
         
        //myrect.origin = myPoint;
        [myrect setOrigin:myPoint];
         
        //NSLog(@"myrect width = %i,height = %i",myrect.width,myrect.height);
        NSLog(@"myrect width = %i,height = %i",[myrect width],myrect.height);
        NSLog(@"origin at (%i,%i)",myrect.origin.x,myrect.origin.y);     //注释[1]
        NSLog(@"area = %i,perimeter = %i",[myrect area],[myrect perimeter]);
            }
    return 0;
}

1、在以上代码中,rectangle的实现代码里面并没有将origin作为@property的成员,因此手写了setOrigin和origin方法,但是为什么在后面使用myrect.origin=mypoint时却自动调用了setOrigin和origin方法?
2、在rectangle实现代码中,{XYpoint *origin}是将origin当成rectangle的成员变量了吗?或者说将origin作为rectangle的属性?
0 0