OC学习之类和对象

定义和C++一样,都需要类的声明和实现,而且头文件进行类声明,.m文件类的实现。
类在@interface和@end之间声明成员变量和方法,在@implementation和@end之间实现方法;

Person.h

@interface Person : NSObject 
{
    // 在大括号内定义成员变量
    // 定义成员变量,默认是@protect
    NSString* name;
    NSInteger age;
    
    // 成员变量访问控制
    @private
    NSString* private_member;
    @protected
    NSString* protected_member;
    @public
    NSString* public_member;
    
}

// 定义属性配对在.m文件里面定义@synthesize
// @property语义:
// nonatomic/atomic
// readonly/readwrite

// assign/week 简单赋值,弱引用
// retain/strong释放旧对象,retain,强引用
// copy 复制新对象
@property(nonatomic, retain) NSString* name;
@property(nonatomic) NSInteger age;

// 以-号开头
// 定义对象方法,初始化方法以initWithXXXX开始
-(id)initWithName:(NSString*)n andAge: (NSInteger)a;
-(void)print;

// 类方法,创建默认对象;以+开头
+(Person*)createPerson;
+(int)createCount;

@end

Person.m

@implementation Person

@synthesize name;
@synthesize age;

// 静态变量
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);
}

// 类方法,创建默认对象;以+开头
+(Person *)createPerson{
    return [[Person alloc] initWithName:@"default" andAge:0];
}

+(int)createCount{
    return count;
}

//做一个标记,以下是实现Action协议
#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 
#import "Person.h"
#import "Employee.h"
#import "PersonSub.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /*
        Person *p = [Person alloc];     // 申请空间
        p = [p initWithName:@"xuzhiming" andAge:33]; // 调用初始化
        [p release];  // 释放空间
        */
        
        // xCode 现在采用ARC,就不需要手动调用release了
        Person *p = [[Person alloc] initWithName:@"xuzhiming" andAge:33];
        // 方法调用
        [p print];
        // 调用分类的方法
        [p newPrint];
        // 属性调用
        NSLog(@"p.name=%@, p.age=%ld", p.name, p.age);
        // 公共成员变量调用
        p->public_member = @"public_member";
        NSLog(@"public_member = %@", p->public_member);
        
        // 判断Person是否实现了Action协议
        if ([p conformsToProtocol:@protocol(Action)]) {
            NSLog(@"Person 遵循 Action协议");
            [p walk];
            [p smile];
        }
        
        // 判断Person是否实现了run方法
        if ([p respondsToSelector:@selector(run)]) {
            [p run];
        }
        else{
            NSLog(@"p没有run方法");
        }
        
        // 类方法调用
        Person *p2 = [Person createPerson];
        [p2 print];
        
        
        Employee *e = [[Employee alloc] initWithName:@"longma" andAge:30 andEducation:@"本科"];
        // Employee的print覆盖了父类的方法,只需要定义一样的方法即可
        [e print];
      
    }
    return 0;
}

输出结果:

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协议
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

你可能感兴趣的:(OC学习之类和对象)