零基础Objective-C 第三章--------------面向对象编程的基础知识(1)

来源:互联网 发布:翻拍小说的网络电视剧 编辑:程序博客网 时间:2024/05/29 08:09

零基础Objective-C 第三章————–面向对象编程的基础知识(1)

Objective-C是一种面向对象的语言(Object-Oriented Programming),在讨论OOP之前,先来看看OOP的一个关键概念:间接(indirection)。

间接

在代码中通过指针简介获取某个值,而不是直接获取。
1. 变量与间接

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSLog(@"The numbers from 1 to 5:");        for(int i = 0; i < 5; i++){            NSLog(@"%d\n",i);        }    }    return 0;}输出2017-06-18 22:03:37.679049+0800 Count_1[1063:72686] The numbers from 1 to 5:2017-06-18 22:03:37.679176+0800 Count_1[1063:72686] 02017-06-18 22:03:37.679187+0800 Count_1[1063:72686] 12017-06-18 22:03:37.679193+0800 Count_1[1063:72686] 22017-06-18 22:03:37.679198+0800 Count_1[1063:72686] 32017-06-18 22:03:37.679203+0800 Count_1[1063:72686] 4Program ended with exit code: 0

假设现在想要更新这个程序,使其能输出1到10的数字, 那必须修改代码中的所有数字5,我们可以用10全局替换所有的5,然后重新构建这个程序。但如果一个上千行的代码程序中有些数字5并不是和这些有关系,那就必须要很谨慎,然后一个一个去替换。
变量就是用来解决此类问题的。

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        //int count = 5;        int count = 10;        NSLog(@"The numbers from 1 to %d", count);        for(int i = 0; i < count; i++){            NSLog(@"%d\n",i);        }    }    return 0;}

通过添加变量,代码更加简洁了,也容易编辑了

2.使用文件名的间接

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        const char *words[4] = {"aardvark", "abacus",            "allude", "zygote"};        int wordCount = 4;        for(int i = 0; i < wordCount; i++){            NSLog(@"%s is %lu characters long", words[i], strlen(words[i]));        }    }    return 0;}2017-06-18 22:32:32.886419+0800 Count_1[1272:92025] aardvark is 8 characters long2017-06-18 22:32:32.886557+0800 Count_1[1272:92025] abacus is 6 characters long2017-06-18 22:32:32.886569+0800 Count_1[1272:92025] allude is 6 characters long2017-06-18 22:32:32.886577+0800 Count_1[1272:92025] zygote is 6 characters longProgram ended with exit code: 0

假设现在希望能使用另一组单词还带(如Joe-Bob “Handyman” Brown),那我们必须去编辑源文件,编辑时还要注意标点符号,转义符的使用等等问题;如果将所有的单词放入代码外的某个文本文件中,每一行一个单词,没错,这就是间接。

main.mint main(int argc, const char * argv[]) {    @autoreleasepool {        FILE *wordFile = fopen("/Users/chenxinsi/code/IOS/Count_1/Count_1/File1", "r");        char word[100];        while(fgets(word, 100, wordFile)){        word[strlen(word) - 1] ='\0';            NSLog(@"%s is %lu characters long", word, strlen(word));        }        fclose(wordFile);    }    return 0;}File1chenxinsizhangsanlisiwangwujay "xinsi" sa输出2017-06-18 23:05:10.241373+0800 Count_1[1428:114888] chenxinsi is 9 characters long2017-06-18 23:05:10.241521+0800 Count_1[1428:114888] zhangsan is 8 characters long2017-06-18 23:05:10.241536+0800 Count_1[1428:114888] lisi is 4 characters long2017-06-18 23:05:10.241547+0800 Count_1[1428:114888] wangwu is 6 characters long2017-06-18 23:05:10.241557+0800 Count_1[1428:114888] jay "xinsi" sa is 14 characters longProgram ended with exit code: 0

fopen()函数打开晚间以便读取内容
fgets()从文件中读取一行文本并将其放入字符数组word中
fgets()调用会保留每行的换行符,但我们并不需要,我们就用\0替换掉。

    3.
阅读全文
0 0