Objective C 快速入门学习二
来源:互联网 发布:中央音乐学院附中 知乎 编辑:程序博客网 时间:2024/05/16 17:55
类、对象、方法
1.编写一个复数类
#import <Foundation/Foundation.h>@interface Complex: NSObject //类声明,Complex继承NSObject{ int iReal;//成员变量声明,在括号内 int iImag;}//成员函数声明,在括号外-(void) print; -(void) setReal : (int) n;-(void)setImag : (int) d;@end //@interface ....@end区域完成类声明 @implementation Complex //成员函数实现部分-(void) print //开头的负号(-)告诉编译器是实例方法,{ NSLog(@"iReal = %i, iImag = %i \n", iReal, iImag);//"\n"和C/C++,表示换行}-(void)setReal : (int) n // 返回值为void,输入参数为int n{ iReal = n;}-(void)setImag: (int) d{ iImag = d;}@end //@implementation ... @end区域完成类成员函数实现
2.使用Complex类
int main(int argc, const char* argv[]){ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //使用方法语法: [ClassOrInstance method] myComplex = [Complex alloc]; myComplex = [Complex init]; //可以合成一句 myComplex = [ [Complex alloc] init] //或者myComplex = [ Complex new] [myComplex setReal : 1];//调用带参数的方法,实部设置为1 [myComplex setImag : 2]; [myComplex print];//调用print方法 [myComplex release];//释放alloc的myComplex [pool drain]; return 0;}
3.自己编程规范
(1).类首个字母大写
(2).函数和变量、对象首个字母小写
(3).变量加上类型修饰,可以区分对象
0 0
- Objective C 快速入门学习二
- Objective-C 快速入门--基础(二)
- Objective C 快速入门学习一
- Objective C 快速入门学习三
- Objective C 快速入门学习四
- Objective C 快速入门学习五
- Objective-C 快速入门
- Objective-C 快速入门
- Objective C 快速入门诗
- Objective-C语法快速入门
- Objective-C语法快速入门
- 学习Objective-C: 入门
- Objective-C学习二
- Objective-C语法快速参考--(入门)
- Objective-C基础语法快速入门
- 【转】Objective C 快速入门诗
- Objective-C基础语法快速入门
- Objective-C 快速入门--基础(一)
- 获取元素在数组中的位置
- Echo服务器
- linux 下开发 segfault 错误调试
- POJ2752 Seek the Name, Seek the Fame 【KMP】
- iOS 配置文档是否可以共享
- Objective C 快速入门学习二
- spring注入与new
- VS2008报错LINK:fatal error LNK1000: Internal error during IncrBuildImage
- 代码从windows下visual studio到andriod平台迁移的修改记录
- unity简单AI
- Spring jar包详解
- ** poj Y2K Accounting Bug 2586
- OpenGL: 绘制按钮
- 点击当行查看按钮,选中当行复选框