OC学习之类和对象

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

Person.h

@interface Person : NSObject <Action>
{
    // 在大括号内定义成员变量
    // 定义成员变量,默认是@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 <Foundation/Foundation.h>
#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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 下面是我最近两年学习OC中的一些基础知识,对于学习OC基础知识的人可能有些帮助,拿出来分享一下,还是那句话不喜勿喷...
    小小赵纸农阅读 7,652评论 1 7
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,719评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,258评论 19 139
  • 离开三江,穿过另一个可能有美妙风景的地方——靖州苗族侗族自治县(那可能又是另一个故事了),一路驶向与广西接壤的湖南...
    孟苏阅读 4,079评论 0 3
  • 【死磕2017最后99天】 8/99 关键词:时间,记录,并行任务 今日阅读:第3章 管理 P62—P88 国庆节...
    花琪儿阅读 1,891评论 2 0

友情链接更多精彩内容