類
定義和C++一樣,都需要類的聲明和實(shí)現(xiàn),而且頭文件進(jìn)行類聲明,.m文件類的實(shí)現(xiàn)。
類在@interface和@end之間聲明成員變量和方法,在@implementation和@end之間實(shí)現(xiàn)方法;
Person.h
@interface Person : NSObject <Action>
{
// 在大括號(hào)內(nèi)定義成員變量
// 定義成員變量,默認(rèn)是@protect
NSString* name;
NSInteger age;
// 成員變量訪問控制
@private
NSString* private_member;
@protected
NSString* protected_member;
@public
NSString* public_member;
}
// 定義屬性配對(duì)在.m文件里面定義@synthesize
// @property語義:
// nonatomic/atomic
// readonly/readwrite
// assign/week 簡單賦值,弱引用
// retain/strong釋放舊對(duì)象,retain,強(qiáng)引用
// copy 復(fù)制新對(duì)象
@property(nonatomic, retain) NSString* name;
@property(nonatomic) NSInteger age;
// 以-號(hào)開頭
// 定義對(duì)象方法,初始化方法以initWithXXXX開始
-(id)initWithName:(NSString*)n andAge: (NSInteger)a;
-(void)print;
// 類方法,創(chuàng)建默認(rèn)對(duì)象;以+開頭
+(Person*)createPerson;
+(int)createCount;
@end
Person.m
@implementation Person
@synthesize name;
@synthesize age;
// 靜態(tài)變量
static int count = 0;
// 常量
const int MAX_COUNT = 30;
-(id)initWithName:(NSString *)n andAge:(NSInteger)a{
// super指向父類
self = [super init];
if (self) {
name = n;
age = a;
}
++count;
return self;
}
-(void)print{
NSLog(@"Person: name=%@, age=%ld", name, age);
}
// 類方法,創(chuàng)建默認(rèn)對(duì)象;以+開頭
+(Person *)createPerson{
return [[Person alloc] initWithName:@"default" andAge:0];
}
+(int)createCount{
return count;
}
//做一個(gè)標(biāo)記,以下是實(shí)現(xiàn)Action協(xié)議
#pragma mark With Action Protocol
-(void)walk{
NSLog(@"Person %@ walk!", name);
}
-(void)smile{
NSLog(@"Person %@ smile", name);
[self privateMethod];
}
// 定義私有方法,不再頭文件中聲明;子類無法訪問
-(void)privateMethod{
NSLog(@"This is privateMethod");
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Employee.h"
#import "PersonSub.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
Person *p = [Person alloc]; // 申請(qǐng)空間
p = [p initWithName:@"xuzhiming" andAge:33]; // 調(diào)用初始化
[p release]; // 釋放空間
*/
// xCode 現(xiàn)在采用ARC,就不需要手動(dòng)調(diào)用release了
Person *p = [[Person alloc] initWithName:@"xuzhiming" andAge:33];
// 方法調(diào)用
[p print];
// 調(diào)用分類的方法
[p newPrint];
// 屬性調(diào)用
NSLog(@"p.name=%@, p.age=%ld", p.name, p.age);
// 公共成員變量調(diào)用
p->public_member = @"public_member";
NSLog(@"public_member = %@", p->public_member);
// 判斷Person是否實(shí)現(xiàn)了Action協(xié)議
if ([p conformsToProtocol:@protocol(Action)]) {
NSLog(@"Person 遵循 Action協(xié)議");
[p walk];
[p smile];
}
// 判斷Person是否實(shí)現(xiàn)了run方法
if ([p respondsToSelector:@selector(run)]) {
[p run];
}
else{
NSLog(@"p沒有run方法");
}
// 類方法調(diào)用
Person *p2 = [Person createPerson];
[p2 print];
Employee *e = [[Employee alloc] initWithName:@"longma" andAge:30 andEducation:@"本科"];
// Employee的print覆蓋了父類的方法,只需要定義一樣的方法即可
[e print];
}
return 0;
}
輸出結(jié)果:
2016-03-10 01:51:22.450 MyOCLearn[2164:74204] Person: name=xuzhiming, age=33
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person newPrint name = xuzhiming
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] p.name=xuzhiming, p.age=33
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] public_member = public_member
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person 遵循 Action協(xié)議
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person xuzhiming walk!
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person xuzhiming smile
2016-03-10 01:51:22.453 MyOCLearn[2164:74204] p沒有run方法
2016-03-10 01:51:22.453 MyOCLearn[2164:74204] Person: name=default, age=0
2016-03-10 01:51:22.453 MyOCLearn[2164:74204] Employee name=longma, age=30, education=本科
2016-03-10 01:51:22.454 MyOCLearn[2164:74204] Employee name=longma, age=30, education=本科
Program ended with exit code: 0