zoukankan      html  css  js  c++  java
  • OC--@property和@synthesize关键字

    //
    //  MyFirstClass.h
    //  Hello Objective-C
    //
    //  Created by admin on 2020/11/16.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Car : NSObject
    
    @property int x,y;
    
    -(void) printx;
    -(void) printy;
    
    @end
    
    
    //
    //  MyFirstClass.m
    //  Hello Objective-C
    //
    //  Created by admin on 2020/11/16.
    //
    
    #import "MyFirstClass.h"
    
    @implementation Car
    -(void) printy{
        NSLog(@"%d", _y);
    }
    
    -(void) printx{
        NSLog(@"%d", _x);
    }
    
    @end

    在interface中对成员变量指定@property关键字,系统在编译的时候会自动加上get和set方法,但注意变量名会转化为带下划线的:x-->_x

    如果不想要下划线 在implementation中加上@synthesize关键字即可:

    //
    //  MyFirstClass.h
    //  Hello Objective-C
    //
    //  Created by admin on 2020/11/16.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Car : NSObject
    
    @property int x,y;
    
    -(void) printx;
    -(void) printy;
    
    @end
    
    
    //
    //  MyFirstClass.m
    //  Hello Objective-C
    //
    //  Created by admin on 2020/11/16.
    //
    
    #import "MyFirstClass.h"
    
    @implementation Car
    @synthesize x,y;
    -(void) printy{
        NSLog(@"%d", y);
    }
    
    -(void) printx{
        NSLog(@"%d", x);
    }
    
    @end
  • 相关阅读:
    用java简单的实现单链表的基本操作
    Pointcut is not well-formed: expecting 'identifier' at character position 0
    Spark 学习(二)
    Spark 学习
    学习Mahout (四)
    github 入门
    Source Insight 入门设置
    shell chpasswd 命令 修改用户密码
    sed 匹配 换行符
    学习Mahout(三)
  • 原文地址:https://www.cnblogs.com/FdWzy/p/14041427.html
Copyright © 2011-2022 走看看