ios 视频学习 3.4 @property属性和点语法

来源:互联网 发布:mac地址绑定错误 编辑:程序博客网 时间:2024/05/29 07:44

在一个类中声明另一个类的对象指针,要在该类的init方法中进行初始化。

#import "House.h"

@interface Person:NSObject

{

    House * house;

    int age;

}

-(void)getAge;

@end


//Person.m

@implementation Person

-(id)init()

{

    if(self=[super init])

    {

         house=[ [ House alloc ] init ];

    }

    return self;

}

-(void)getAge

{

    age=30;

    return age;

}

@end

//新建一个House类

@interface House:NSObject

{

    int area;

}

-(void)getArea;

@end

//Person.m

@implementation House

    -(void)getArea

    {

        area=30;

        return area;

    }

@end

其中,init方法已经在父类中声明。

打印类名的方法:

NSLog("self类名称:%@",[self class]);//当前类名

NSLog("父类名称:%@",[self superclass]);//当前类名


下面介绍@property和@synthesize声明。

先声明int numer;

@property (nonatomic,readonly)  int numer;

@synthesize number;

nonatomic意思是非原子的(这个线程没有把number上锁,其他线程可以访问它),atomic意思是原子的(这个线程把number锁定)。

readonly代表只读,生成number方法,不生成setNumber:(int) 方法

用property声明number以后,在方法里面使用,需要用self.number使用。(直接用number应该也可以)


疑问:assign retain的意义。

0 0