目前移动开发热火朝天,今天在家学习最近入手的《Objective-c基础教程》,初步感觉Objective-c与.Net差别很大,为了更好的学习理解Objective-c,将以笔记的形式记录学习的知识点,方便查阅。
在Objective-c中类分为两部分:声明和实现,下面定义一个Person类,
Person.h:
#import<Foundation/Foundation.h>@interfacePerson : NSObject
{//实例成员变量声明@PRivateNSString*_firstName;@protectedNSString*_lastName;@publicNSInteger*_age;
}//属性声明@property (nonatomic,strong) NSString *firstName;
@property (nonatomic,strong) NSString*lastName;
@property (nonatomic,assign) NSInteger*age;//实例方法-(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age;-(void)printFullName;//类方法+(void)breath;@end
Person.m
#import"Person.h"@implementationPerson-(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age
{if(self =[super init])
{
self.firstName=firstName;
self.lastName=lastName;
self.age=age;
}returnself;
}-(NSString *)description
{return[[NSString alloc]initWithFormat:@"firstName: %@,lastName:%@,age:%zd",self.firstName,self.lastName,self.age];
}+(void)breath
{
NSLog(@"people breathing");
}-(void)printFullName
{
NSLog(@"%@",self->_firstName);
}@end
通过程序得出结论如下:
1. #import引入头文件,该命令可以保证头文件只被包含一次,无论此命令在文件中出现多少次,c语言使用#include命令。
2. 每个方法前面都有一个"+" 或者 "-"符号,+表示该方法为类方法由类调用,-表示该方法为实例方法由实例调用。
3. self关键字表引用实例对象自身,与.NET中的this关键字功能相似。
3. NSObject是Objective-c中基类,自定义类建议继承NSObject,Objective-C不支持多继承,但是可以通过category(类别) 和 protocol(协议)实现多继承的效果
4. description方法继承与NSObject,在Person中被重写,在Objective-c中如果直接使用实例对象,默认调用description方法,相当于.NET中的ToString。
5. 在Objective-c中方法调用在一对方括号之间,形式如:[ instance method : parameter parameter ......]。
6. super用于调用超类中的实现方式,被用于作为调用目标。
7. 类的定义使用关键字@interface,实现使用关键词@implementation,
8. 属性的声明使用关键字@property,格式如:@property (attribute1 attribute2) type propertyName;
9.@private、@public、@protect用户声明私有、公有、受保护的成员变量,
测试Person类:
Person *person = [[Person alloc]initWithFirstName:@"first"lastName:@"last"age:25];
NSLog(@"%@",person);//firstName: first,lastName:last,age:25[person printFullName];//irst last[Person breath];//people breathing
objective-c中得属性可以使用如下关键字进行修饰:
线程安全性:nonatomic, atomic[默认]
属性读写性:readonly, readwrite[默认]
其他特性:assign vs retain[strong] vs weak vs unsafe_unretained vs copy
assign[默认],主要用于非指针类型的属性成员,基本数据类型
strong 和retain功能相同,引用计数+1,主要用于指针类型的属性成员
weak: 声明为weak的指针,指针指向的地址一旦被释放,这些指针都将被赋值为nil,有效的防止野指针
unsafe_unretained:声明为unsafe_unretained的指针,指针指向的地址一旦被释放,其他指向该地址的指针都将成为野指针。所以程序开发中尽量少用。
copy:创建一个副本,引用计数为1
objective-c中协议是包含一组函数定义的集合,继承协议的类型需要实现协议中定义的接口,如下已定一个PersonDelegate协议
@protocolPersonDelegate <NSObject>//必须实现的方法@required-(void)sleep;-(void)eat;//可实现,也可不实现的方法@optional-(void)playBasketBall;@end
现在就让Person继承PersonDelegate,
Person.h:
#import<Foundation/Foundation.h>@protocolPersonDelegate <NSObject>//必须实现的方法@required-(void)sleep;-(void)eat;//可实现,也可不实现的方法@optional-(void)playBasketBall;@end@interfacePerson : NSObject<PersonDelegate>{//实例成员变量声明@privateNSString*_firstName;@protectedNSString*_lastName;@publicNSInteger*_age;
}//属性声明@property (nonatomic,strong) NSString *firstName;
@property (nonatomic,strong) NSString*lastName;
@property (nonatomic,assign) NSInteger*age;//实例方法-(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age;-(void)printFullName;//类方法+(void)breath;//PersonDelegate 方法-(void)sleep;-(void)playBasketBall;
//未实现eat方法
@end
Person.m:
#import"Person.h"@implementationPerson-(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age
{if(self =[super init])
{
self.firstName=firstName;
self.lastName=lastName;
self.age=age;
}returnself;
}-(NSString *)description
{return[[NSString alloc]initWithFormat:@"firstName: %@,lastName:%@,age:%zd",self.firstName,self.lastName,self.age];
}+(void)breath
{
NSLog(@"people breathing");
}-(void)printFullName
{
NSLog(@"%@ %@",self->_firstName,self->_lastName);
}//PersonDelegate 方法-(void)sleep
{
NSLog(@"Person sleeping...");
}-(void)playBasketBall
{
NSLog(@"Person Play basketball");
}@end
测试代码:
Person *person = [[Person alloc]initWithFirstName:@"first"lastName:@"last"age:25];
[person sleep];//Person sleeping...[person playBasketBall];//Person play basketball
通过测试总结如下几点:
1. 一个协议可以实现另一个协议,如要实现多个协议,采用逗号隔开:<protocol1,protocol2....>。
2. @require定义的方法表示必须要求实现,@optional定义的方法表示可以不实现,但是经过测试发现,协议中得方法在类没有任何强制要求实现任何方法。
以上就是开始学习Objective-C的初步理解,如有不正确的地方,请指出。