Objective -C Object initialization 对象初始化

Objective -C Object initialization 对象初始化

1.1 Allocating Objects  分配对象

Allocation is the process by which a new object is born.

allocation 是新对象诞生的过程。

Sending the alloc message to a class causes that class to allocate a chunk of memory large enough to hold all its instance variables.

发送alloc 消息能够让class分配足够多得内存。

All your BOOLs start out as NO;

all your ints are 0; all your floats become 0.0; all your pointers are nil;

A newly allocated object isn‘t ready to be used right away: you need to initialize it before you can work with it.

一个刚分配的对象还不能用,你需要初始化它。

Some languages, including C++ and Java, perform object allocation and initialization in a single operation using a constructor. Objective-C splits the two operations into explicit allocation and initialization stages.

java和C++执行allocation and initialization 在一个操作里。

而Objective-C分开了两个操作。

1.2 Initializing objects 对象初始化

Initialization takes a chunk of memory and gets it ready to become a productive member of society. init methods—that is, methods that do initialization—almost always return the object they‘re initializing.

初始化让其成为对象的一员。init methods 也就是说总是返回要被创建的对象。

Car *car = [[Car alloc] init];

1.3 Writing initializationg methods  写初始化方法

(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

The first bit of code that runs in that statement is [super init].That code lets the superclass do its initialization work.

[super init]允许父类完成初始化工作。

For classes that inherit from NSObject, calling on the superclass lets NSObject do any processing it needs to do so that objects can respond to messages and deal with retain counts.

对于任何继承自NSObject的类,初始化能够让对象相应详细和处理retain counts .

For classes that inherit from another class, this is their chance to do their own version of clean-slate initialization.

对于继承自其他类的类,它有机会初始化自身。

Remember that instance variables are found at a memory location that‘s a fixed distance from the hidden self parameter.

记住在实例变量发现在内存的位置,固定位置能够找到隐藏的self参数。

If a new object is returned from an init method, we need to update self so that any subsequent instance variable references affect the right places in memory.

如果一个对象返回一个init 方法,我们需要更新self 才能在任何子变量中适当的位置找到实例变量。

An init method can return nil if there‘s a problem initializing an object.

一个初始化方法可能返回nil 。

Keep in mind that this assignment affects the value of self only for this method. It doesn‘t change anything outside the method‘s scope.

记住这个分配只影响self 在本方法内。不会影响其他方法。

The test if (self = [super init]) won‘t run the body code if nil is returned from [super init].

这个测试不会运行,如果[super init ]返回为nil的话。

An init method returns the object that was just initialized. Since we assigned the return value of [super init] to self, that‘s what we should return.

一个正确地初始化方法返回一个初始化对象。因为我们把[super init ]赋值给self ,所以返回self

1.4 What to Do When You‘re Initializing

This is the place to do your clean-slate initialization work. You assign values to instance variables and create the other objects that your object needs to do its work.

在这里,你可以做一些从头再来的工作。你分配值和创建你需要的其他对象.初始化需要做什么工作还是看你自己的需求。

1.5 Convenience initializers

In fact, it‘s important to remember that init methods are nothing special. They‘re just ordinary methods that follow a naming convention.

init methods没什么特殊的。他们也是普通的类。

Many classes have convenience initializers. These are init methods that do some extra work, saving you the trouble of doing it yourself.

许多类提供简单方法。做额外的工作。

- (id) initWithFormat: (NSString *) format, ...;

这个也是初始化方法。

时间: 2024-08-27 17:29:43

Objective -C Object initialization 对象初始化的相关文章

prototype linkage can reduce object initialization time and memory consumption

//对象是可变的键控集合, //“numbers, strings, booleans (true and false), null, and undefined” 不是对象的解释 The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined. All other values are objects. Numbers, strings, and boolea

理解隐式类型、对象初始化程序和匿名类型

在C# 3.0中,几乎每个新特性都是为LINQ服务的.所以,本文将介绍下面几个在C# 3.0中引入的新特性: 自动实现的属性 隐式类型的局部变量 对象和集合初始化程序 隐式类型的数组 匿名类型 其实这几个特性都是比较容易理解的,对于这几个特性,编译器帮我们做了更多的事情(想想匿名方法和迭代器块),从而简化我们的代码. 自动实现的属性 在C# 3.0以前,当我们定义属性的时候,一般使用下面的代码 public class Book { private int _id; private string

Java对象初始化详解

出处:http://blog.jobbole.com/23939/ 在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一点是Java规范规定的.本文试图对Java如何执行对象的初始化做一个详细深 入地介绍(与对象初始化相同,类在被加载之后也是需要初始化的,本文在最后也会对类的初始化进行介绍,相对于对象初始化来说,类的初始化要相对简单一 些). 1.Java对象何时被初始化 Java对象在其被创建时初始化,在Java代码中,有两种行为可以引起对象的创建.其中比较直观的一种,也就是通常所

Spring-----配置及对象初始化(1)

一,配置文件进行Spring初始化 1,配置文件编写 <?xml version="1.0" encoding="utf-8" ?> <configuration> /************* 这里会报异常“Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常.把配置注释掉就行了 <startup> <supportedRuntime version="v4.0

jQuery对象初始化的多种传参数形式

jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第二个参数context指定上下文,其中context参数可以为$(...), DOMElement3.$(function() {}); <===> $(document).ready(function() { });4.$({selector : '.class

Java对象初始化详解(转)

在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一点是Java规范规定的.本文试图对Java如何执行对象的初始化做一个详细深入地介绍(与对象初始化相同,类在被加载之后也是需要初始化的,本文在最后也会对类的初始化进行介绍,相对于对象初始化来说,类的初始化要相对简单一些). 1.Java对象何时被初始化 Java对象在其被创建时初始化,在Java代码中,有两种行为可以引起对象的创建.其中比较直观的一种,也就是通常所说的显式对象创建,就是通过new关键字来调用一个类的构造函数,通过构造函

{{jQuery源码分析}}jQuery对象初始化的多种传参数形式

jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第二个参数context指定上下文,其中context参数可以为$(...), DOMElement3.$(function() {}); <===> $(document).ready(function() { });4.$({selector : '.class

聚焦:jQuery对象初始化的多种传参数形式

今天学习了jQuery对象初始化的传参方式,和大家分享一下: jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第二个参数context指定上下文,其中context参数可以为$(...), DOMElement3.$(function() {}); <===> $(document).ready(function

C++中对象初始化

在C++中对象要在使用前初始化,永远在使用对象之前先将它初始化. 1.对于无任何成员的内置类型,必须手工完成此事. 例如: int x=0; double d; std::cin>>d; 2.内置对象以外的东西,初始化责任落在构造函数身上.确保每一个构造函数都将对象的每一个成员初始化. 例如: class Point {......}; class Point3d { public: Point3d(Point pt,int z); private:                   Poi