objective-C概念总结

2016-03-23  本文已影响52人  fakepinge

面向对象编程(oop)

定义类:
类的声明部分:

@interface 类名 : 父类名 {
    //成员变量(数据抽象)
}
// 属性(数据抽象)
// 初始化方法
// 方法(行为抽象)
@end

类的实现部分:

@implementation 类名
// 方法的实现

创建对象:

类名 *对象指针 = [[类名 alloc] 初始化方法];

发消息:

[对象指针 方法名];
返回可I型 变量名 = [对象指针 方法标签:参数];
[对象指针 方法标签1:参数1 标签2:参数2];

例子:

#import <Foundation/Foundation.h>

// Step 1. 定义类
// 数据抽象: 找到和类(对象)相关的属性(找名词)
// 行为抽象: 找到和类(对象)相关的方法(找动词)

// 类的声明部分
@interface Circle : NSObject {
    // 数据抽象
    @private double _radius;
}


// 类方法  +号 通过类名直接调用(给类发消息)
// 对象方法 -号 必须先创建对象才能调用(给对象发消息)

+ (instancetype) circleWithRadius:(double) radius;

// 初始化方法
//- (instancetype) initWithRadius:(double) radius;

// 行为抽象

- (double) perimeter;

- (double) area;

@end

// 类的实现部分
@implementation Circle

// 在类方法中self代表类
+ (instancetype)circleWithRadius:(double)radius {
    return [[self alloc] initWithRadius:radius];
}

// 在对象方法中self代表对象
- (instancetype) initWithRadius:(double) radius {
    if (self = [super init]) {
        _radius = radius;
    }
    return self;
}

- (double) perimeter {
    return 2 * M_PI * _radius;
}

- (double) area {
    return M_PI * _radius * _radius;
}

@end

static const double WALL = 5.5;
static const double AISLE = 7.8;

int main() {
    // 自动释放池(离开释放池申请的内存会做一次释放操作)
    @autoreleasepool {
        printf("请输入游泳池的半径: ");
        double r;
        scanf("%lf", &r);
        // Step.2 创建对象
        // 用中缀符语法给对象发消息
        Circle *smallCircle = [Circle circleWithRadius:r];
        Circle *bigCircle = [Circle circleWithRadius:r + 3];
        // Step.3 给对象发消息求解问题
        printf("The price of aisle is $%.2f\n",
               ([bigCircle area] - [smallCircle area]) * AISLE);
        printf("The price of wall is $%.2f\n", [bigCircle perimeter] * WALL);
    }
    return 0;
}

系统常用类

类别(Category)

 给已有的类打一个补丁包,增强它的功能。
 一个类方法比属性更重要。
#import <Foundation/Foundation.h>

@interface NSString (Util)

- (instancetype) reverse;

- (instancetype) encodeWithKey:(NSInteger) key;

- (instancetype) decodeWithKey:(NSInteger) key;

@end

#import "NSString+Util.h"

@implementation NSString (Util)

- (instancetype) reverse {
    NSMutableString *mStr = [NSMutableString string];
    for (NSInteger i =  self.length - 1; i >= 0; i--){
        unichar ch = [self characterAtIndex:i];
        [mStr appendFormat:@"%C", ch];
    }
    return [mStr copy];
}

- (instancetype) encodeWithKey:(NSInteger) key {
    NSMutableString *mStr = [NSMutableString string];
    for(int i = 0; i < self.length; i++){
        unichar ch = [self characterAtIndex:i];
        ch = ch ^ key;
        [mStr appendFormat:@"%C", ch];
    }
    return [mStr copy];
}

- (instancetype) decodeWithKey:(NSInteger) key {
    return [self encodeWithKey:key];
}
@end

#import <Foundation/Foundation.h>
#import "NSString+Util.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"我恨你中国";
        NSLog(@"%@", [str reverse]);
        NSString *encodedStr = [str encodeWithKey:100];
        NSLog(@"%@", encodedStr);
        NSLog(@"%@", [encodedStr decodeWithKey:100]);
    }
    return 0;
}

![屏幕快照 2016-03-23 20.52.31.png](https://img.haomeiwen.com/i1787713/b6132b4f2c60a787.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

提示:在Category定义的方法命名时,最好加上命名前缀,例如命名类的前缀时ZP,那么Category中的方法应加上zp_前缀,这样做主要是为了让调用者能够区分是苹果原生的方法还是Category新加的方法。

属性(Property)

//一个顶三个

@property (属性修饰符) 类型 变量名;

在默认的情况下,属性会自动合成成员变量以及setter(修改器)、getter(访问器)方法,如果不愿意使用自动合成,也可以在类的实现部分(.m)通过@synthesize指令指定属性如何合成。

说明:如果需要向外部暴露对象的某个属性就是用@property,这样可以通过点语法来操作这个属性;如果不想暴露对象的某个属性(将数据保护起来)就应该直接定义成员变量。

上一篇 下一篇

猜你喜欢

热点阅读