您的位置:首页 > 移动开发 > Objective-C

01-Objective-c简介

2014-04-17 16:19 387 查看

一、 OC简介

  1.oc 程序执行过程:



C语言的基础上,增加了一层最小的面向对象语法

完全兼容C语言

可以在OC代码中混入C语言代码,甚至是C++代码

可以使用OC开发Mac OS X平台和iOS平台的应用程序

实例代码:

1>.完全兼容C语言:

// 在main.m文件里

#include <stdio.h>
int main()
{
printf("OC程序的入口main函数\n");
return 0;
}

// 终端指令
// 编译:  cc  -c  main.m
// 链接:  cc main.o
// 执行:  ./a.out


  2>.只含OC代码,不含有C代码:

#import <Foundation/Foundation.h>


//  #import 和 #include 的区别 
//  跟#include一样,用来拷贝某个文件的内容
//  可以自动防止文件内容被拷贝多次,也就意味着头文件中不用加入下面的预处理指令
//    #ifndef 	_STDIO_H_
//    #define	_STDIO_H_
//    #endif


  

int main()
{
NSLog(@"第2个OC程序");
return 0;
}
// 终端指令
// 编译cc –c main.m
// 链接cc main.o –framework Foundation
// 运行./a.out


  3>.OC和C混用

1)    编写3个文件  main.m  one.h one.c
➢    main.m
#import "test.h"
int main()
{
test();
return 0;
}

➢    test.h
void test();

➢    test.c
#include <stdio.h>
#include "test.h"
void test()
{
printf("调用了test函数\n");
}

2)    终端指令
➢    编译:cc –c main.m test.c
➢    链接:cc main.o test.o
➢    运行:./a.out
(没有使用Foundation框架的话,就不用-framework Foundation)


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: