Objective-C 语法
来源:互联网 发布:tomylife是什么软件 编辑:程序博客网 时间:2024/06/06 06:20
下面是一个描述Objective-C类的使用的demo
#import <Foundation/Foundation.h>@interface Fractions: NSObject-(void) print;-(void) setNumerator: (int) n;-(void) setDenominator: (int) d;@end@implementation Fractions{ int numerator; int denominator;}-(void)print{ NSLog(@"%i/%i", numerator, denominator);}-(void)setNumerator:(int)n{ numerator = n;}-(void)setDenominator:(int)d{ denominator = d;}@endint main(int argc, const char * argv[]) { @autoreleasepool { Fractions *myFraction; myFraction = [Fractions alloc]; myFraction = [myFraction init]; [myFraction setNumerator:1]; [myFraction setDenominator:3]; NSLog(@"The value of my fraction is:"); [myFraction print]; } return 0;}
程序分成三个部分:
- @interface
- @implementation
- program
The @interface Section
当你想定义一个新的类,你要告诉编译器你的你的类是从哪里来的,并定义类的方法和属性。
@interface NewClassName: ParentClassName propertyAndMethodDeclarations;@end
类方法:
-(void) setNumerator: (int) n;
(-)号表示这是一个实例方法,即new了之后才可以使用的方法。与之对应的时(+),这表示类本身的方法,此类方法通常应用在类本身。
The @implementation Section
这是一部分包含你在@interface部分声明方法的具体实现。
@implementation NewClassName{ memberDeclarations; } methodDefinitions; @end
@implementation Fractions{ int numerator; int denominator;}
这里面numerator和denominator是类的属性,一旦类实例化就会生成。
The program Section
这一部分包含了你用来解决实际问题的代码。
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 语法1
- objective-c语法
- C#命名空间、类型的别名管理
- BCB 用TImage对图片像素放缩
- This message is already in use错误
- 高清显示屏原理及设计方案
- Android开发在路上:少去踩坑,多走捷径
- Objective-C 语法
- linux tr命令详解
- JdbcTemplate查询
- ip cap 包判断是否支持断点续传
- C#中类的使用举例
- Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等
- Windows安装Git客户端工具
- 看好你的门-确保验证机制的安全(1)-使用可靠的敏感信息
- POJ——1001 Exponentiation 解题思路