iOS:不同屬性聲明方式的解析

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   ar   div   代碼   sp   

代碼:

/*屬性聲明方式說明: ----------------------- 1@interface ...{    id name}@end 這樣聲明的屬性其實可以認為是private屬性,因為它只能在方法裡通過name引用,外部無法通過“object.name”的方式進行引用 (內部也不能通過self引用)----------------------  2@interface ...@property id name@end這樣聲明的屬性可以認為是public屬性,內部通過“self.name”引用;外部通過“object.name”引用 ----------------------- 3@implementation ...@synthesize name@end 允許以property聲明的屬性,在內部方法裡除了以self引用外,還可以用屬性名稱直接引用 ----------------------- 4self.name其實是是兩個方法(setter,getter):id value = [self name][self setName = ...] ----------------------- 5總結:如果架構依賴的屬性是@property,就不能實現為私人,否則程式可以崩掉 */#import <Foundation/Foundation.h>@interface Student : NSObject{    NSString *name;}@property NSString *address;-(void) toString;@end@implementation Student@synthesize address;-(Student *)init{    name = @"lishujun";    self.address = @"beijing";    return self;}-(void) toString{    NSLog(@"%@ %@", name, [self address]);}@endint main(int argc, const char * argv[]){    @autoreleasepool {                // insert code here...        Student *student = [[Student alloc]init];        [student toString];        [student setAddress:@"tongzhou"];        [student toString];        NSLog(@"%@", student.address);                // 語法錯誤,沒有setName這個方法        //[student setName:@"shujunli"];        //[student toString];            }    return 0;}

 

iOS:不同屬性聲明方式的解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.