黑马程序员——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
- 黑马程序员——Objective-C学习笔记(七) :初始化对象
- 黑马程序员——Objective-C学习笔记(一):面向对象
- 黑马程序员——Objective-C学习笔记(八): 属性
- 黑马程序员 — Objective-C学习笔记(二):继承
- 黑马程序员——Objective-c学习笔记——类(面向对象)——黑马 ios 技术博客
- [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第十章 对象初始化
- 黑马程序员---Objective-C学习笔记之一
- 黑马程序员——Objective--C笔记Foundation之NSArray,集合对象的内存管理
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之03-类、对象和方法——黑马 IOS 技术博客
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之18-复制对象——黑马 IOS 技术博客
- 黑马程序员—Objective-C学习—基本语法、类和对象
- 黑马程序员—Objective-C学习—类和对象的内存管理
- 【黑马程序员】iOS学习之路——Objective-C之面向对象
- 黑马程序员——IOS基础---Objective-C第一段视频学习---举例说明面向对象思想
- 黑马程序员——IOS基础---Objective-C第二段视频学习---面向对象---基本语法
- 黑马程序员——Objective-C中的类与对象
- 黑马程序员——Objective-C中的类和对象
- 黑马程序员--Objective-C——面向对象
- 黑马程序员_main()方法的理解
- Android 5.x Theme 与 ToolBar 实战
- 黑马程序员_java中代码块的概念--单态设计模式
- 使用Android Studio阅读整个Android源码
- sfdhgsfdgfdsfddgsfhgfdsgf
- 黑马程序员——Objective-C学习笔记(七) :初始化对象
- java web后台开发新手常见问题
- 解决Fedora升级时nvidia显卡问题
- P123 第30题 求杨辉三角的第39行第19列的数
- js图片实时加载提供网页打开速度
- log4j-总结-1
- Java 内存分配全面浅析
- 基于插入的排序算法
- kyeremal-网络流24题T6-最长递增子序列问题