2010-04-11 55 views
8

我試圖通過Objective-C教程的方式工作。在這本書中有這樣的例子:Objective-C getter/setter

@interface 
{ 
int width; 
int height; 
XYPoint *origin; 
} 
@property int width, height; 

我想,「嘿有針對的Xypoint對象中沒有的getter/setter的代碼不工作,雖然」現在我要去回答我自己的問題:)。

我認爲它是因爲「出身」是一個指針已經和什麼有「寬度」和「高度」引擎蓋下發生,是有打算TE創建一個指向他們..

上午我的權利,還是我說BS :) ??

我只是不知道它。這裏的主:

#import "Rectangle.h" 
#import "XYPoint.h" 
int main (int argc, char *argv[]) 
{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    Rectangle *myRect = [[Rectangle alloc] init]; 
    XYPoint *myPoint = [[XYPoint alloc] init]; 

    [myPoint setX: 100 andY: 200]; 
    [myRect setWidth: 5 andHeight: 8]; 

    myRect.origin = myPoint; 

    NSLog (@"Rectangle w = %i, h = %i", 
      myRect.width, myRect.height); 

    NSLog (@"Origin at (%i, %i)", 
      myRect.origin.x, myRect.origin.y); 

    NSLog (@"Area = %i, Perimeter = %i", 
      [myRect area], [myRect perimeter]); 

    [myRect release]; 
    [myPoint release]; 
    [pool drain]; 

    return 0; 
} 

而這裏的矩形對象:

#import "Rectangle.h" 
#import "XYPoint.h" 

@implementation Rectangle 
@synthesize width, height; 

-(void) setWidth: (int) w andHeight: (int) h 
{ 
    width = w; 
    height = h; 
} 

- (void) setOrigin: (XYPoint *) pt 
{ 
    origin = pt; 
} 
-(int) area 
{ 
    return width * height; 
} 
-(int) perimeter 
{ 
    return (width + height) * 2; 
} 
-(XYPoint *) origin 
{ 
    return origin; 
} 
@end 

什麼我不明白的是這一行主營:myRect.origin = myPoint;我並沒有爲它制定者..

BTW感謝爲您的快速回復

回答

20

什麼我不明白的是這條線在主:有myRect.origin = myPoint;我沒有做一個二傳手吧..

既是一個getter和一個setter(統稱爲存取)在Rectangle類中爲origin創建。如果你在爲Rectangle實施一看,這是消氣:

-(XYPoint *) origin 
{ 
    return origin; 
} 

,這是二傳:

- (void) setOrigin: (XYPoint *) pt 
{ 
    origin = pt; 
} 

,並作爲Objective-C的2.0呼叫:

myRect.origin = myPoint; 

相當於:

[myRect setOrigin:myPoint]; 

使用@property聲明getter和setter方法(然後使用@synthesize實現它們)正在申報和創建存取的只有一條路,而且是有一個方便的,如果你有大量的屬性在類接口聲明。作爲Schildmeijer說,@property int width是等於宣告了兩個方法:

- (int)width; 
- (void)setWidth:(int)newWidth; 

由於Objective-C的方法調用的動態綁定的性質,你甚至沒有申報的getter和setter方法界面,儘管如果您將這些界面廣告爲其他類別公開可用,那麼這樣做通常是最佳做法。

6

您可以將屬性聲明視爲等價於聲明兩個存取方法。因此

@property int width; 

等同於:

- (int)width; 

- (void)setWidth:(int)newWidth; 
0

你不說什麼代碼工作,或者你的期望是「工作」是什麼。

上述接口將創建簡單的寬度和高度訪問器方法,可以從其他對象調用[object setWidth:1];object.width = 1; - 這兩個類似。

原產地是一些其他的對象類型,是一個指針,是的。但是你仍然想聲明一個屬性來生成訪問器方法。

0

如果您需要從另一個類訪問實例變量,或者使用綁定來獲取/設置它們,則獲取器和設置器非常有用。所以我的猜測是,你需要這個功能的寬度和高度,但不是原點。請注意,getters/setters不會在整數中指出指針,因爲您聲明的可能是原因。 Ints是整數,getter/setter不會改變它。

0
//Rectangle.h 
#import <Foundation/Foundation.h> 
@interface Rectangle : NSObject 
@property int Width; 
@property int Height; 
-(int)Area; 
@end 

//Rectangle.m 
#import "Rectangle.h" 
@implementation Rectangle 
@synthesize Width;/*Will create value Width , Setter called"setWidth" and Getter called "Width"*/ 
@synthesize Height;/*Will create value Height , Setter called"setHeight" and Getter called "Height"*/ 

-(int)Area 
{ 
    return Width*Height; 
} 
@end 


// main.m 
#import <Cocoa/Cocoa.h> 
#import "Rectangle.h" 
int main(int argc, const char * argv[]) 
{ 
    Rectangle *myRectangle = [Rectangle new]; 

    myRectangle.Width=3; 
    myRectangle.Height=5; 
    printf("Area = %d\n",[myRectangle Area]); 

    //Or 
    [myRectangle setWidth:5]; 
    [myRectangle setHeight:6]; 
    printf("Area = %d\n",[myRectangle Area]); 

} 

如果你想吸氣劑只或重命名getter和setter

•只讀

•吸氣= newGetterName

•二傳手=新SetterName

例如

//Rectangle.h 
#import <Foundation/Foundation.h> 
@interface Rectangle : NSObject 
@property (getter = getWidth) int Width; 
@property (readonly) int Height; 
@end