@property(assign) int age;//assign
基本数据类型
@property(copy) NSString
*name;//copy
字符串
@property(nonatomic,retain) Father *father;//多线程访问不安全,但效率高
Father(类)
@property(atomic,retain) Father *father2;//多线程访问安全
Father(类)
在 .m 文件中使用 @synthesize 编译器指令
@synthesize
age;
@synthesize name;
@synthesize father;
@synthesize father2;
在编译过程中,系统会自己添加相当于(java的set和get方法)
协议与委托
一、先创建一个协议,在协议写方法
二、创建一个委托者的类,在类名后面写<协议名>,然后在此类的实现中重写协议的方法
三、创建一个雇主类,在雇主类中写委托者的方法,在实现中实现方法,方法写的是委托者自己回调自己的方法。
四、在main函数中创建雇主类和委托者类的对象,雇主把协议给委托者,最后雇主调用委托者的方法。
下面有个实例:
1、协议(teachdelegate.h)
#import
@protocol teachdelegate
-(void)teachIos:(int)num;//协议中的方法
@end
2、委托类
工人类( Worker.h)
#import
#import "teachdelegate.h"
@interface Worker : NSObject
@end
工人类的实现(Worker.m)
#import "Worker.h"
@implementation Worker
//重写协议中的方法
-(void)teachIos:(int)num{
NSLog(@"教%d个学生",num);
}
@end
3、雇主
老板类(Boss.h)
#import
#import "teachdelegate.h"
@interface Boss : NSObject
@property(retain,nonatomic) id delegate;
-(void)teachStudentIos;//工人的方法
@end
老板类的实现(Boss.m)
#import "Boss.h"
@implementation Boss
|