Objective-C 语法一(类)
来源:互联网 发布:java数组如何调用方法 编辑:程序博客网 时间:2024/05/29 18:04
Objective-C 语法一(类)
首先申明下,本文为笔者学习《Objective-C 基础教程》的笔记,并加入笔者自己的理解和归纳总结。
1、#import语句
#import可保证头文件只被包含一次。#import <Foundation/Foundation.h>
2、NSLog和@"字符串"
NSLog方法与printf相似,向控制台输出。@符号表示NSString类型。
3、布尔类型(BOOL)
YES和NO4、类
@interface表示这是类的接口,以@end结束。@implementation实现类定义的接口,以@end结束。
每个类都是NSObject。
在{}内定义类需要的各种数据成员。
短线"-"声明是类方法,后面紧跟返回类型,void表示无返回值。
方法参数中()定义参数类型,紧跟参数名。
@interface ShapeBounds : NSObject {int width, height;}- (void) setWidth: (int)width height: (int)height;@end@implementation ShapeBounds- (void) setWidth: (int)w height: (int)h {width = w;height = h;}- (NSString*) description {return [NSString stringWithFormat: @"(%d, %d)", width, height];}@endint main(int argc, const char* argv[]) {ShapeBounds* bounds = [ShapeBounds new];[bounds setWidth: 100 height: 60];NSLog(@"%@", bounds); // (100, 60)}
5、继承
类声明时,冒号":"后面的标识符是需要继承的类,Objective-C只支持单继承。super关键字能调用父类的方法。
@interface Shape : NSObject - (void) draw;@end@implementation Shape- (void) draw {NSLog(@"draw Shape");}@end@interface Circle : Shape@end@implementation Circle- (void) draw {[super draw];NSLog(@"draw Circle");}@endint main(int argc, const char* argv[]) {Circle* circle = [Circle new];[circle draw];}
6、复合
Shape拥有自己的位置,可以用ShapeBounds表示。@interface Shape : NSObject {ShapeBounds* bounds;}- (void) setBounds: (ShapeBounds*) bounds;- (void) draw;@end@implementation Shape- (void) setBounds: (ShapeBounds*) b {bounds = b;}- (void) draw {NSLog(@"draw Shape at %@", bounds);}@endint main(int argc, const char* argv[]) {ShapeBounds* bounds = [ShapeBounds new];[bounds setWidth: 100 height: 60];Circle* circle = [Circle new];[circle setBounds: bounds];[circle draw];}
7、跨文件依赖
ShapeBounds可以拆分为ShapeBounds.h和ShapeBounds.mShapeBounds.h文件
#import <Foundation/Foundation.h>@interface ShapeBounds : NSObject {int width, height;}- (void) setWidth: (int)Width height: (int)height;@endShapeBounds.m文件
#import "ShapeBounds.h"@implementation ShapeBounds- (void) setWidth: (int)w height: (int)h {width = w;height = h;}- (NSString*) description {return [NSString stringWithFormat: @"(%d, %d)", width, height];}@endShape类分为Shape.h和Shape.m,在Shape中引用了ShapeBounds类,必须在文件中声明依赖关系。
@class可以声明引用关系,在编译时知道这是一个类,但ShapeBounds被修改时不用重新编译Shape.h文件。
Shape.h文件
#import <Foundation/Foundation.h>@class ShapeBounds;@interface Shape : NSObject {ShapeBounds* bounds;}- (void) setBounds: (ShapeBounds*) bounds;- (void) draw;@endShape.m文件
#import "Shape.h"#import "ShapeBounds.h"@implementation Shape- (void) setBounds: (ShapeBounds*) b {bounds = b;}- (void) draw {NSLog(@"draw Shape at %@", bounds);}@endCircle类分为Circle.h和Circle.m,Circle.h文件中Shape类不能用@class声明。
Circle.h文件
#import "Shape.h"@interface Circle : Shape@endCircle.m文件
#import "Circle.h"@implementation Circle- (void) draw {[super draw];NSLog(@"draw Circle");}@endmain.m文件
#import "Circle.h"#import "ShapeBounds.h"int main(int argc, const char* argv[]) {ShapeBounds* bounds = [ShapeBounds new];[bounds setWidth: 100 height: 60];Circle* circle = [Circle new];[circle setBounds: bounds];[circle draw];}
阅读全文
0 0
- Objective-C 语法一(类)
- Objective-C基础语法(一)
- objective-c 语法 细节(一)
- Objective-C基础语法(一)
- Objective-C语法系列(一)
- Objective-C基础语法(一)
- LHF Objective-C语法(2)类
- Objective-C 基础语法log打印那些事儿(一)
- Objective-C 基础语法log打印那些事儿(一)
- Objective-C 基础语法log打印那些事儿(一)
- Objective-C研究院之基础语法(一)
- Objective-C 、Java、C++的语法对比 (一)
- ios培训 Objective-C语法大讲堂(一)
- Objective-C 基础语法log打印那些事儿(一)
- objective-c 编程基础(一 基础语法)
- Objective-C Copy语法(一)对于Objective-C 提供的类对象NSString/NSMutableString; NSArray/NSMutableArray...
- 【Objective-C】语法(一)类、实例化、函数(方法)
- objective c之中的类和实例,方法等基本语法(一)
- 【报表福利大放送】100余套报表模板免费下
- ios转场动画
- 经验分享:为什么你10年经验依旧平庸,他人却2年成就卓越?
- C#调用C++类
- 【机器学习】python实践笔记 -- 经典监督学习模型之分类学习模型
- Objective-C 语法一(类)
- 微信小程序之遍历(四)
- 命名实体识别方法
- MySQL解决插入emoji表情失败的问题
- spring事务属性的几个试验(转载)
- 适配器模式(Adapter)(对象结构型模式)
- recycleview万能分割线
- Kotlin学习笔记——标准库kotlin.collections
- 开机自动打开浏览器并定时重启