• [Objective-c 基础


    Xcode编译器的特性,自动生成getter和setter
     

    A.@property

    自动生成某个成员变量的getter和setter的声明
    变量的命名要求:以下划线开头
    复制代码
     1  
     2 Student.h
     3 @interface Student : NSObject
     4 {
     5     int _age;
     6     int _no;
     7     int age;//此变量不会被访问到
     8     double height;
     9     NSString *_name;
    10 }
    11 
    12 @property int age, no;
    13 @property int height;
    14 @property NSString *name;
    15 
    16 @end
    复制代码
     
    B.@synthersize
    自动生成getter和setter的实现
    1 @implementation Student
    2 
    3 @synthesize age = _age, no = _no, height = _height;//指定一下成员变量,否则会访问同名变量,如果不存在,就会自动生成@private的变量
    4 @synthesize name = _name;
    5 
    6 @end
    C.省略变量的声明,在.h中得声明可以省略
    注意:XCode自动生成的成员变量都是@private
     
    复制代码
    1 @interface Student : NSObject
    2 
    3 @property int age;
    4 @property double height;
    5 @property NSString *name;
    6 
    7 @end
    复制代码

    D.省略实现(XCode4.4以上)
    只需要写@property,默认访问下划线开头的成员变量
    1 @interface Car : NSObject
    2 
    3 @property int wheels;
    4 
    5 @end
     
    E.如果手动实现了setter和getter,编译器就不再自动生成下划线开头的成员变量
    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    NOI2019 I 君的商店
    CF1326F
    APIO2016 划艇
    LeetCode-Remove Nth Node From End of List
    LeetCode-Remove Element
    LeetCode-Remove Duplicates from Sorted List II
    LeetCode-Remove Duplicates from Sorted List
    LeetCode-Unique Paths II
    LeetCode-Unique Paths
    LeetCode-Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/4501555.html
Copyright © 2020-2023  润新知