黑马程序员——Objective-C学习笔记(七) :初始化对象

来源:互联网 发布:excel表格查找重复数据 编辑:程序博客网 时间:2024/06/05 12:01

 ——- android培训、IOS培训、期待与您交流! ———-
 

分配对象

分配(allocation)是一个新对象诞生的过程。向某个类发送 alloc 消息,就能为类分配一块足够大的内存,以存放该类的全部实例对象。同时alloc 方法还顺便将这块内存区域全部初始化为0.
刚分配的对象并不能立即使用,需要先初始化,然后才能使用。只执行分配而未进行初始化的对象可能会出现bug。

初始化对象

与分配对应的操作是初始化。init方法一般都会返回七正在初始化的对象。

调用的正确方式如下:
Car *car = [[Car alloc] init];

编写初始化方法

(id) init{    if (self = [super init])    {        engine = [Engine new];        tires[0] = [Tire new];        tires[1] = [Tire new];        tires[2] = [Tire new];        tires[3] = [Tire new];    }    return (self);} // init

重点在第一行:

`if (self = [super init])`

声明中最先运行的代码是[super init],作用是让超类完成七自身的初始化。self参数是通过固定的距离寻找实例变量的内存位置的。如果从init方法返回一个新对象,则需要更新self,以便其后的实例变量的引用可以被映射到正确的内存位置。这个赋值操作只影响该init方法中self的值,而不影响该方法范围以外的任何内容。init方法最后一行代码是return (self);init方法返回了刚刚已经被初始化的对象。

Tire类的初始化

类声明如下:

#import <Cocoa/Cocoa.h>@interface Tire: NSObject{    float pressure;    float treadDepth;}- (void) setPressure: (float) pressure;- (float) pressure;- (void) setTreadDepth: (float) treadDepth;- (float) treadDepth;@end // Tire

Tire类的实现:

#import "Tire.h"@implementation Tire-(id) init{     if (self = [super init])    {        pressure = 34.0;        treadDepth = 20.0;    }    return (self);} // init- (void) setPressure: (float) p{    pressure = p;} // setPressure- (float) pressure{    return (pressure);} // pressure-(void) setTreadDepth:(float) td{    treadDepth = td;} // treadDepth- (NSString) description{    NSString *desc;    desc = [NSString stringWithFormat: @"Tire: Pressure: %.1f TreadDepth: %.1f",pressure, treadDepth];    return (desc);} // description@end // Tire

main()函数:

#import "Engine.h"#import "Car.h"#import "Slant6.h"#import "AllweatherRadial.h"int main(int argc,const char *argv[]){    @autoreleasepool    {        Car *car = [[Car alloc] init];        for (int i=0; i<4; i++)        {            Tire *tire;            tire = [[Tire alloc] init];            [tire setPressure: 23+i];            [tire setTreadDepth: 33-i];            [car setTire: tire atIndex: i];            [tire release];        }        Engine *engine = [[Slant6 alloc] init];        [car setEngine: engine];        [car print];        [car release];    }    return (0);} // main

Car类

#import <Cocoa/Cocoa.h>@class Tire;@class Engine;@interface Car: NSObject{    NSMutableArray: *tires;    Engine *engine;}- (void) setEngine: (Engine *) newEngine;- (Engine *) engine;- (void) setTire: (Tire *) tire atIndex: (int) index;- (Tire *) tireAtIndex: (int) index;- (void) print;@end // Car
#import "Car.h"@implementation Car- (id) init{    if (self = [super init])    {        tires = [[NSMutableArray alloc] init];        for (init i=0; i<4; i++)        {            [tires addObject: [NSNull null]];        }    }    return (self);} // init- (void) setEngine: (Engine *) newEngine{    [newEngine retain];    [engine release];    engine = newEngine;} // setEngine- (Engine *) engine{    return (engine);} // engine- (void) setTire: (Tire *) tire atIndex: (int) index{    [tire replaceObjectAtIndex: index withObject: tire];} // setTire: atIndex:- (Tire *) tireAtIndex: (int) index{    Tire *tire;    tire = [tires objectAtIndex: index];    return (tire);} // tireAyIndex:- (void) dealloc{    [tires release];    [engine release];    [super dealloc];} // dealloc// 一定要调用超类的dealloc方法,并且要确保[super dealloc]是dealloc方法的最后一条语句- (void) print{    for (int i=0; i<4; i++)    {        NSLog(@"%@", [self tireAtIndex: i]);    }    NSLog (@"%@", engine);} // print@end // Car
0 0
原创粉丝点击