zoukankan      html  css  js  c++  java
  • @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方法也不会自动生成

  • 相关阅读:
    下载文件c#
    系统蓝屏重起:如何修改设置,记录系统蓝屏重起的错误
    Repeater中的行数
    2010617 重装系统遇到的问题
    ajax 修改select的值 返回的值中有逗号
    GPS数据接收 串口调试感受
    Nios II实用之音频控制
    【笔记】VB控件MSComm功能介绍
    【摘】程序员版同桌的你
    AJAX Control Toolkit ——DragPanelExtender(拖拽面板)
  • 原文地址:https://www.cnblogs.com/imChay/p/5590393.html
Copyright © 2011-2022 走看看