Creating Instances
类对象[class object]的一个主要功能就是创建新的对象实例.类名称代表类对象.
A principal function of a class object is to create new instances. This code tells the Rectangle class to create a new rectangle instance and assign it to the myRectangle variable:
id myRectangle;
myRectangle = [Rectangle alloc]; //调用类对象创建该类的实例
The alloc method dynamically allocates memory for the new object’s instance variables and initializes them all to 0—all, that is, except the isa variable that connects the new instance to its class. For an object to be useful, it generally needs to be more
completely initialized. That’s the function of an init method. Initialization typically follows immediately after allocation:
myRectangle = [[Rectangle alloc] init];
alloc 方法为新创建的对象动态分配内存,并初始化为0,isa由于要关连他的类,所以不会初始化为0,通常我们需要调用init 方法做更加多的初始花工作.
This line of code, or one like it, would be necessary before myRectangle could receive any of the messages that were illustrated in previous examples in this chapter. The alloc method returns a new instance and that instance performs an init method to set
its initial state. Every class object has at least one method (like alloc)
that enables it to produce new objects, and every instance has at least one method (like init) that prepares it for use. Initialization methods often take parameters to allow particular values to be passed and have keywords to label the parameters (initWithPosition:size:,
for example, is a method that might initialize a new Rectangle instance), but every initialization method begins with “init”.
在myRectangle 对象接收任何消息之前,这行代码,myRectangle = [[Rectangle alloc] init]; 是很必要的,alloc 为实例分配内存,init 设置对象的初始状态.每一个类对象至少有一个象alloc 的方法去生产新实例,而每一个实例,对象实例至少有一个象init的方法去初始话对象的状态,初始话方法常带参数.但任何的初始话方法都都是以init开始的.
Customization with Class Objects [Page 27]
这段基本是想表达,如何订制一个类对象使其能满足不同的需求., 一种办法是定义抽象类,然后不同的需求定义不同的子类,但这中做法会随着需求增多,类会越来越多.
另外一种比较好的方法是让对象实例在初始化时接受一个类对象做参数.同时提供方法可以改变这个类对象.
[myMatrix setCellClass:[NSButtonCell class]]; //出入不同的类对象.
Variables and Class Objects
When you define a new class, you can specify instance variables. Every instance of the class can maintain its own copy of the variables you declare—each object controls its own data. There is, however, no class variable counterpart to an instance variable.
Only internal data structures, initialized from the class definition, are provided for the class. Moreover, a class object has no access to the instance variables of any instances; it can’t initialize, read, or alter them.
每一个类的实例对象都拥有一份我们在类定义中的实例变量的拷贝,但是并不存在类变量的概念,对应于实例变量..只有内部的数据结构在类定义时候初始化,
再者,类对象是不能访问对象的实例变量的,不能初始化他们,读取等
For all the instances of a class to share data, you must define an external variable of some sort. The simplest way to do this is to declare a variable in the class implementation file:
int MCLSGlobalVariable;
@implementation MyClass
// implementation continues 想让所有的类实例对象共享数据,我们可以简单的在类的实现文件中定义一个某数据结构的外部变量.
In a more sophisticated implementation, you can declare a variable to be static, and provide class methods to manage it. Declaring a variable static limits its scope to just the class—and to just the part of the class that’s implemented in the file. (Thus
unlike instance variables, static variables cannot be inherited by, or directly manipulated by, subclasses.) This pattern is commonly used to define shared instances of a class (such as singletons; see “Creating a Singleton Instance” in Cocoa Fundamentals
Guide ).
而通常的做法是定义一个static 的变量,并提供类方法去管理,这种定义的是在类范围的.而且还是在类定义文件的范围. 因此不能被继承,不能被其子类直接操作.
这种模式通常用来定义共享的实例变量,例如单例模式.
static MyClass *MCLSSharedInstance;
@implementation MyClass
+ (MyClass *)sharedInstance
{
// check for existence of shared instance
// create if necessary
return MCLSSharedInstance;
}
// implementation continues
Static variables help give the class object more functionality than just that of a factory producing instances;
it can approach being a complete and versatile [多用途的;多功能的]object in its own right. A class object can be used to coordinate the instances it creates, dispense instances from lists of objects already created, or manage other processes
essential to the application. In the case when you need only one object of a particular class, you can put all the object’s state into static variables and use only class methods. This saves the step of allocating and initializing an instance.
Note It is also possible to use external variables that are not declared static, but the limited scope of static variables better serves the purpose of encapsulating data into separate objects.
Initializing a Class Object
If you want to use a class object for anything besides allocating instances, you may need to initialize it just as you would an instance. Although programs don’t allocate class objects, Objective-C does provide a way for programs to initialize them.
除了分配实例外,如果我们想使用类对象,则需要初始话类对象,虽然程序上没分配类对象,但Object C 提供了方法初始化.
If a class makes use of static or global variables, the initialize method is a good place to set their initial values. For example, if a class maintains an array of instances, the initialize method could set up the array and even allocate one or two default instances to have them ready.
如果一个类使用静态或者全局的变量,则初始化方法(initialize)是一个设置初始值的好地方.
The runtime system sends an initialize message to every class object before the class receives any other messages and after its superclass has received the initialize message. This sequence gives the class a chance to set up its runtime environment before
it’s used. If no initialization is required, you don’t need to
write an initialize method to respond to the message.
运行时系统在每一个类接收任何消息前都会先发送一个初始化消息:initialize,一直到他的父类已经接收到这个消息. 这个顺序给类在使用前一个机会去设置一些运行时信息.如果没什么需要
初始话就不需要写初始化方法去响应这个消息了.
Because of inheritance, an initialize message sent to a class that doesn’t implement the initialize method is forwarded to the superclass, even though the superclass has already received the initialize message. For example, assume class A implements the initialize
method, and class B inherits from class A but does not implement the initialize method. Just before class B is to receive its first message, the runtime system sends initialize to it. But, because class B doesn’t implement initialize, class A’s initialize
is executed instead. Therefore, class A should ensure that its initialization logic is performed only
once, and for the appropriate class.
初始化方法不被继承,,如过A实现了初始话方法,B 继承A,但不继承A的初始话方法,在B接收第一个消息前,运行时系统发送初始话消息给B,但由于B没有初始话消息,A 代替执行.,由于Run time system 会分别发送initialize 消息给每一个类,所以A在代替执行B的初始化方法前,其实已经收到过一个initialize 消息. 所以要保证初始化正确,且只初始化一次.
因此A要确保初始化保证执行一次,在正确的类上
To avoid performing initialization logic more than once, use the template in Listing 1-1 when implementing
the initialize method.
Listing 1-1 Implementation of the initialize method
+ (void)initialize //注意是+ ,表示属于类对象的方法
{
if (self == [ThisClass class]) {
// Perform initialization here.
...
}
}
Note Remember that the runtime system sends initialize to each class individually. Therefore,in a class’s implementation of the initialize method, you must not send the initialize messageto its superclass.
Methods of the Root Class
所以对象包括类,实例都需要提供一个接口给runtime system,NSObject 就是这样的接口,可以让类对象,实例具有内省他们的能力,和报告他们在继承链种的位置.
All objects, classes and instances alike (同样的), need an interface to the runtime system. Both class objects and instances should be able to introspect (V.内省) about their abilities and to report their
place in the inheritance hierarchy. It’s the province (范围, 职责) of the NSObject class to provide this interface.
此处不知道是原文有误,按目前猜测,:NSObject 方法需要被继承两次,一次是提供接口运行时接口给实例,然后是重复一套接口给类对象[class object],
So that NSObject methods don’t have to be implemented twice—once to provide a runtime interface for instances and again to duplicate that interface for class objects—class objects are given special dispensation to perform instance methods defined in the
root class. When a class object receives a message that it can’t respond to with a class method, the runtime system determines whether there’s a root instance method that can respond. The only instance methods that a class object can perform are those defined
in the root class,and only if there’s no class method that can do the job. //需要继续研究
For more on this peculiar ability of class objects to perform root instance methods, see NSObject Class Reference .
理解Objective-C中类对象的创建与初始化

被折叠的 条评论
为什么被折叠?



