zoukankan      html  css  js  c++  java
  • OC property(声明)

    @interface Student : NSObject {
        int _age;
        int _no;
        float _height;
    }
    
    // 当编译器遇到@property时,会自动展开成getter和setter的声明
    @property int age;
    //- (void)setAge:(int)newAge;
    //- (int)age;
    
    @property int no;
    //- (void)setNo:(int)newNo;
    //- (int)no;
    
    @property float height;
    //- (void)setHeight:(float)newHeight;
    //- (float)height;
    
    
    - (void)test;
    @end

    // 在xcode4.5的环境下,可以省略@synthesize,并且默认会去访问_age这个成员变量
    // 如果找不到_age这个成员变量,会自动生成一个叫做_age的私有成员变量

    @implementation Student
    
    // @synthesize age, height, no;
    
    // @synthesize会自动生成getter和setter的实现
    
    // @synthesize默认会去访问跟age同名的变量
    // 如果找不到同名的变量,会自动生成一个私有的同名变量age
    // @synthesize age;
    
    // age = _age代表getter和setter会去访问_age这个成员变量
    @synthesize age = _age;
    //- (void)setAge:(int)newAge {
    //    _age = newAge;
    //}
    //
    //- (int)age {
    //    return _age;
    //}
    
    @synthesize height = _height;
    //- (void)setHeight:(float)newHeight {
    //    _height = newHeight;
    //}
    //
    //- (float)height {
    //    return _height;
    //}
    
    @synthesize no = _no;
    //- (void)setNo:(int)newNo {
    //    _no = newNo;
    //}
    //
    //- (int)no {
    //    return _no;
    //}
    
    - (void)test {
        
        _age = 10;
        
        
        _height = 10.0f;
        
        _no = 10;
        
    }
    @end
  • 相关阅读:
    AGC007题解
    博弈论学习笔记
    ZROI2019 提高十连测
    男人八题2019
    LOJ 2840「JOISC 2018 Day 4」糖
    CF671D Roads in Yusland
    网络流套路小结
    BZOJ 3729 GTY的游戏
    AGC036C GP 2
    BZOJ 5046 分糖果游戏
  • 原文地址:https://www.cnblogs.com/liuwj/p/6899915.html
Copyright © 2011-2022 走看看