zoukankan      html  css  js  c++  java
  • @property和@synthesize

    main.m

    #import <Foundation/Foundation.h>

    #import "Student.h"

    int main(int argc, constchar * argv[]) {

        @autoreleasepool {

            Student *stu = [[Studentallocinit];

            

            stu.age = 11;

            NSLog(@"age is %i",stu.age);

        }

        return 0;

    }

     
    Student.h

    #import <Foundation/Foundation.h>

    @interface Student : NSObject{

        //默认是@protected

        int age;

        int age1;

        int age2;

        

        

        int no;

        

        float height;

    }

      //当编译器遇到@property时,会自动展开成gettersetter的声明  一行相当于两行  @property不可以省略

    @propertyint age;

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

    //-(int)age;

    @propertyint no;

    //-(void)setNo:(int)newNo;

    //-(int)no;

    @propertyfloat height;

    //-(void)setHeight:(int)newHeight;

    //-(float)height;

     

    @end

     
    Student.m

    #import "Student.h"

    @implementation Student       //xcode4.5及之后的版本,可以省略@synthesize,并且默认去访问_age这个成员变量,如果找不到会自动生成一个叫_age的私有成员变量

                                  //@synthesize age,height,no;也可以这样写

    @synthesize age;              //@synthesize会自动生成gettersetter的实现

    //-(void)setAge:(int)newAge{  //@synthesize 是默认访问跟 “synthesize <#property#>”中与“<#property#>” 相同的成员变量,如果找不到就会

    //    age = newAge;           //生成一个私有的同名变量 <#property#>

    //}

    //-(int)age{

    //    return age;

    //}

    @synthesize no;                   //如果已经定义了成员变量int _no,则需要这样写:@synthesize no = _no;代表gettersetter去访问_age这个成员变量;之后

    //-(void)setNo:(int)newNo{        //变量no不存在了!!!

    //    no = newNo;

    //}

    //-(int)no{

    //    return no;

    //}

    @synthesize height;

    //-(void)setHeight:(float)newHeight{

    //    height = newHeight;

    //}

    //-(float)height{

    //    return height;

    //}

    @synthesize wide;                  //@synthesize wide;表明已在.h文件中声明了一个成员变量 wide,即不用再声明成员变量wide了。

    @end

  • 相关阅读:
    深入了解spring(二) IOC容器
    深入了解spring(一) spring的作用
    深入了解Java虚拟机(二)
    关于scroll、client、offset和style中的height、width、top以及bottom属性
    打开桌面上的图标就会弹出"打开些文件可能会对您的计算机有害"解决方案
    Windows中区位码转换为机内码
    Servlet 上传图片
    Java&Quartz实现任务调度
    JavaMail
    JavaWeb 二维码
  • 原文地址:https://www.cnblogs.com/GhostKZShadow/p/5105207.html
Copyright © 2011-2022 走看看