• 第二讲:ObjC 点语法


    转:http://tigercat1977.blog.163.com/blog/static/2141561122012111292920505/

    第二讲:Obj-C 点语法 

    2012-12-12 09:29:55|  分类: Objective-C |  标签:objective-c  |字号 订阅

    主要内容
          为什么需要点语法、点语法的好处是什么。
          @property    @systhesize  如何使用
          @property    @systhesize  编译器如何展开
          如何使用点语法
          @property  其他属性

    为什么要设计点语法
          为了方便别的程序员转到 OC 开发上来
          为了让程序设计简单化
          隐藏了内存管理细节
          隐藏了多线程、同步、加锁细节

    点语法
          属性可以在不使用括号的情况下使用点语法
          无需调用 [foo value] 可以使用 foo.value 来访问虽然 foo.value 看起来像是直接访问 value 变量,
          但是属性始终调用方法,而这些方法又可以访问对象的数据。

    Dog.h 头文件申明
          @interface Dog:NSObject
          {
               int  age;
          }
          - (void) setAge:(int)newAge;
          - (int) age;
          @end

    Dog.m 实现
          # import "Dog.h"
          @ implementation Dog
          - (void) setAge:(int) newAge
          {
                age = newAge;
          }
          - (int) age
          {
               return age;
          }
          @ end

    setter 和 getter 函数
          - (void) setAge:(int) newAge;
          - (int) age;
          类似于 setAge: 设置新值一般叫做 setter 函数
          类似于 age 取值一般叫做 getter 函数

    使用经典调用和点语法
          Dog * dog = [[Dog alloc] init];
          [dog setAge: 100];
          int dogAge = [dog age];
          NSLog(@" Dog Age is %d", dogAge);

          dog.age = 200;
          dogAge = dog.age;

          NSLog(@"Dog new Age is %d", dogAge);

    点语法是编译器级别
         dog.age = 200;
         dogAge = [dog age];
         编译器会把 dog.age = 200;
         展开成    [dog setAge:200];

         会把      dogAge = dog.age;
         展开成  dogAge = [dog age];
         函数调用


    点语法 setter 和 getter 规范
         setter 函数展开规范
           dog.age = 200;
           [dog setAge:200];

         getter 函数展开规范
            int dogAge = dog.age;
            int dogAge = [dog age];

    举例子

    // Dog.h

    @interface Dog : NSObject { int age; } // setter 和 getter 函数 - (void) setAge:(int)newAge; - (int) age; @end


    // Dog.m

    #import "Dog.h" @implementation Dog - (void) setAge:(int)newAge { age = newAge; } - (int) age { return age; } @end


    // main.m #import "Dog.h" #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { NSLog(@"Hello, World!"); Dog *dog = [[Dog alloc] init]; [dog setAge:100]; int dogAge = [dog age]; NSLog(@"dog age is %d", dogAge); // 经典方式 dog.age = 200; // 相当于[dog setAge:200] dogAge = dog.age; // 相当于dogAge = [dog age]; NSLog(@"dog new age is %d", dogAge); } return 0; }

    /* 输出结果:

    Hello,World!

    dog age is 100

    dog new age is 200

    */


    ××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
    @property
          @property 是让编译器自动残生函数申明

          不用写下面两行代码
          - (void) setAge:(int) newAge;
          - (int) age;

          只需要下列一行就可以代替
          @property int age;

    Dog.h 头文件申明(使用@property)

    @interface Dog:NSObject { init age; } - (void) setAge:(int) newAge; // 这行不要,下边@prooerty 代替 - (int) age; // 这行不要,下边@prooerty 代替 @property int age; // 由这行来代替 @end


    @synthesize
          @synthesize 就是编译器自动实现 getter 和 setter 函数
          不用写下列代码
          - (void) setAge:(int) newAge
          {
                age = newAge;
          }
          - (int) age
          {
                return age;
          }

          只需要写
          @synthesize age;

    Dog.m 使用 @synthesize

    @implementation Dog @synthesize age; // 这行代替下边6行 - (void) setAge:(int)newAge{ // 这行不要,被@synthsize 代替 age = newAge; // 这行不要,被@synthsize 代替 } // 这行不要,被@synthsize 代替 - (int) age{ // 这行不要,被@synthsize 代替 return age; // 这行不要,被@synthsize 代替 } // 这行不要,被@synthsize 代替 @end


    举例子  (@property  @synthesize)

    // Dog.h @interface Dog : NSObject { int age; } @property int age; @end


    // Dog.m #import "Dog.h" @implementation Dog @synthesize age; @end


    // main.m #import "Dog.h" #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { NSLog(@"Hello, World!"); Dog *dog = [[Dog alloc] init]; [dog setAge:700]; int dogAge = [dog age]; NSLog(@"dog age is %d", dogAge); // 经典方式 dog.age = 800; // 相当于[dog setAge:800] dogAge = dog.age; // 相当于dogAge = [dog age]; NSLog(@"dog new age is %d", dogAge); } return 0; }

    /* 输出结果

    Hello, World!

    dog age is 700

    dog new age is 800

    */


    ××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
    总结

    第二讲:Obj-C 点语法 - tigercat1977 - tiger notes

    getter and setter
           属性一般需要提供赋值方法 setter 和取值方法 getter
           赋值方法用于设置信息,取值方法用以获取信息,可以使用任意名字定义赋值方法和取值方法,
    也可以使用 OC 约定的方法。
          属性前面带有 set 前缀的设置方法,比如
          @property int age; [在类申明中]
          @synthesize age   [在类实现中]

    类内部 age 和 self.age 是不一样的
          在 Dog 类内部可以直接访问 age 变量
          直接访问 age 相当于 age 字段
          self.age 是调用 getter 或者 setter 函数

    @property 进一步说明
          @interface Dog:NSObject {
                int _age;
          }
          @property int age;
          @end

          @synthesize age = _age;

          目的是避免函数名和字段重复
    第二讲:Obj-C 点语法 - tigercat1977 - tiger notes

    Dog.m @synthesize 展开实现
          #import "Dog.h"
          @implementation Dog
          - (void) setAge:(int)newAge{
                _age = newAge;
          }
          - (int) age{
                return _age;
          }
          @end

    属性可以是:
         readwrite (缺省),readonle
                表示属性是可读写的,也就是可以使用 getter 和 setter,而 readonly 只能使用 getter
          assign (缺省),retain, copy
                表示属性如何存储
         nonatomic
                表示不用考虑线程安全问题
         getter = ... , setter = ....
                重新设置 getter 函数和 setter 函数名
    第二讲:Obj-C 点语法 - tigercat1977 - tiger notes

    举例子  (@property  @synthesize   _ )

    // Dog.h @interface Dog : NSObject { int _age; } @property int age; @end


    // Dog.m #import "Dog.h" @implementation Dog @synthesize age = _age; @end


    // main.m

    #import "Dog.h" #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { NSLog(@"Hello, World!"); Dog *dog = [[Dog alloc] init]; [dog setAge:1100]; int dogAge = [dog age]; NSLog(@"dog age is %d", dogAge); dog.age = 2200; dogAge = dog.age; NSLog(@"dog new age is %d", dogAge); } return 0; } /* 输出结果: Hello, World dog age is 1100 dog new age is 2200 */

  • 相关阅读:
    窗体句柄总是0
    该函数设置由不同线程产生的窗口的显示状态。
    zip压缩解压缩 项目icsharpcode-SharpZipLib-e012155
    VS(Microsoft Visual Studio2010)工具打开项目所需的应用程序,出现未安装(.csproj)的应用程序的解决办法
    Cocoa深入学习:NSOperationQueue、NSRunLoop和线程安全
    iPhone 6出现后,如何将一份设计稿支持多个尺寸?
    iPhone/Mac Objective-C内存管理教程和原理剖析
    What does the “__block” keyword mean?
    UIView frame, bounds and center
    ARC __bridge modifiers demystified
  • 原文地址:https://www.cnblogs.com/jackljf/p/3589254.html
Copyright © 2020-2023  润新知