• @property和@synthesize


    //  @property和@synthesize--编译器特性,自动生成get、set方法

    //成员变量

    //int _age;

    //get、set方法声明的快速实现

    //@property int age 等价于

    //- (void)setAge:(int)age;

    //- (void)age;

    //注意使用@property进行get、set方法进行声明的时候不要加上下划线

    //@property int _age;

    //加上下划线,get、set方法的声明就变成这样了

    //- (void)set_age:(int)age;

    //- (void)_age;

    //这样使用点语法的时候就会出现问题了

    //p.age 就会调用方法 [p setAge:10]或者 [p age];

    //@synthesize 快速生成get 、set方法的实现

    //出现一个问题

    //int _age;

    //int age;

    //@property int age

    //@synthesize age _age;//这样写的意思就是,给@property的声明实现方法

    //然后访问成员变量 _age;

    //build 编译

    //简单的写法1

    //@property int wheel,speed;

    //@synthesize wheel = _wheel,speed = _speed;(成员变量没有声明会自动的生成)

    //简单的写法最终版(Xcode4.4以后)

    //@property 即声明get、set方法,也实现get、set方法,没有成员变量还会自动的生成

    @property int age;//缺点是成员变量是私有的,子类不能访问

    //如果想让子类能够访问,加上

    //@protected

    //int _age;

    //@synthesize age 默认会访问 int age ;这个成员变量

    //如果没有age,就会自动的生成@private int age;

    //@property只能写在interface中

    //@synthesize 只能写在 implementation中

    //xcode 如果有了成员变量不会自动生成,如果有了set/get方法也不会自动生成

  • 相关阅读:
    IOS开发调用系统相机和打开闪光灯
    iOS 判断输入字符串长度函数
    Mac下Cocos2d和Cocos2dx安装
    同样的一辈子,不同的结果!
    Mac OS 和 iOS 操作系统架构
    摄影师张梦格App项目总结
    Mac下WorkPress平台的安装与搭建
    dagger与spring注解对比
    git rebase 命令参数
    初识Devexpress ChartControl 之 动态添加stepline及TextAnnotation
  • 原文地址:https://www.cnblogs.com/imChay/p/5590393.html
Copyright © 2020-2023  润新知