《Objective-C基础教程》学习笔记第三-六章

1. 用+和-区分类class method和instance method

2.类的声明和实现:类声明用@interface, 类实现用@implementation

// --------------------------------------------------
// All about Circles
@interface Circle : NSObject
{
  ShapeColor  fillColor;
  ShapeRect   bounds;
}

- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;
@end // Circle

@implementation Circle
- (void) setFillColor: (ShapeColor) c
{
fillColor = c;
} // setFillColor

- (void) setBounds: (ShapeRect) b
{
bounds = b;
} // setBounds

- (void) draw
{
NSLog (@"drawing a circle at (%d %d %d %d) in %@",
   bounds.x, bounds.y, 
   bounds.width, bounds.height,
   colorName(fillColor));
} // draw
@end // Circle

3. 用:分隔符来传递参数,:后面是参数。如下实例,第一个:之后是instance method的参数

- (void) setColorToRed: (float)red Green: (float)green Blue:(float)blue; /* 宣告方法 */
 
[myColor setColorToRed: 1.0 Green: 0.8 Blue: 0.2]; /* 呼叫方法 */

4. 在Objective-C中类的创建有两种:

(1)用中括号和new关键字,无参构造时可用

id shape;
shape = [Circle new];

(2)用alloc和init
id shape;
shape = [[Circle alloc] init]

5. 调用类的函数称为发消息,用[]完成

Car *car = [Car new];
Engine *engine = [Engine new];
[car setEngine: engine];

6. Objective-C的基本数据类型(BOOL, char, double, float, unsigned int, int, unsigned short, short)是值传递,其余均是reference传递。id是一种特殊的void型对象指针,使用时需要强制转换

7. 常用关键字总结:

(1)@是对C的扩展,包括:@interface, @implementation, @protocal, @optional, @required, @end, @encode

(2)nil表示Null

(3)id是Objective-C中的对象指针,指向任意一个对象,和void *不同的是void*并不确定指向的类型

(4)self等同于C++中的this

(5)Super是指向父类的指针

(6)NULL相关的类:nil是一个对象空指针,Nil是一个类空指针,NSNull在集合对象中表示一个空值的对象

8. 在组织源代码时,建议把interface和implementation分别放在.h和.m中,避免产生额外comiple dependency

9. 使用#import来获取某个类的全部功能,使用@class只引用该类的名称(比如在interface定义中的继承和组合,如果不需要知道所用interface的功能,可以用@class代替#import)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值