object c 學習(二) : Objects


2. Object-c 中Objects的含義?

<Objective-C 基礎教程> 中的解釋: 
OOP-------Object-Oriented Programming   Objects 就好比計算機中的小機器, 他們相互交談,協作完成任務.

<The Object-C Programming Language>中的解釋:

As the name implies, object-oriented programs are built around objects. An object associates data with the particular operations that can use or affect that data. Objective-C provides a data type to identify an object variable without specifying a particular class of the object—this allows for dynamic typing.

Object Basics

An object associates data with the particular operations that can use or affect that data. In Objective-C, these operations are known as the object’s methods; the data they affect are its instance variables (in other environments they may be referred to as ivars or member variables). In essence, an object bundles a data structure (instance variables) and a group of procedures (methods) into a self-contained programming unit.

In Objective-C, an object’s instance variables are internal to the object; generally, you get access to an object’s state only through the object’s methods (you can specify whether subclasses or other objects can access instance variables directly by using scope directives, see “The Scope of Instance Variables” (page 40)). For others to find out something about an object, there has to be a method to supply the information. For example, a Rectangle would have methods that reveal its size and its position.

Moreover, an object sees only the methods that were designed for it; it can’t mistakenly perform methods intended for other types of objects. Just as a C function protects its local variables, hiding them from the rest of the program, an object hides both its instance variables and its method implementations.

Runtime 13 2010-07-13 | © 2010 Apple Inc. All Rights Reserved.

14

CHAPTER 1

Objects, Classes, and Messaging

id

In Objective-C, object identifiers are a distinct data type: id. This is the general type for any kind of object regardless of class, and can be used for both instances of a class and class objects themselves.

id anObject;
For the object-oriented constructs of Objective-C, such as method return values, id replaces int as the

default data type. (For strictly C constructs, such as function return values, int remains the default type.) The keyword nil is defined as a null object, an id with a value of 0. id, nil, and the other basic types of

Objective-C are defined in the header file objc/objc.h.

id is defined as pointer to an object data structure:

typedef struct objc_object {
    Class isa;

} *id;
All objects thus have an isa variable that tells them of what class they are an instance. Since the Class type

is itself defined as a pointer:

typedef struct objc_class *Class;
the isa variable is frequently referred to as the “isa pointer.”

Dynamic Typing

The id type is completely nonrestrictive. By itself, it yields no information about an object, except that it is an object. At some point, a program typically needs to find more specific information about the objects it contains. Since the id type designator can’t supply this information to the compiler, each object has to be able to supply it at runtime.

The isa instance variable identifies the object’s class—what kind of object it is. Objects with the same behavior (methods) and the same kinds of data (instance variables) are members of the same class.

Objects are thus dynamically typed at runtime. Whenever it needs to, the runtime system can find the exact class that an object belongs to, just by asking the object. (To learn more about the runtime, see Objective-C Runtime Programming Guide.) Dynamic typing in Objective-C serves as the foundation for dynamic binding, discussed later.

The isa variable also enables objects to perform introspection—to find out about themselves (or other objects). The compiler records information about class definitions in data structures for the runtime system to use. The functions of the runtime system use isa, to find this information at runtime. Using the runtime system, you can, for example, determine whether an object implements a particular method, or discover the name of its superclass.

Object classes are discussed in more detail under “Classes” (page 23).

It’s also possible to give the compiler information about the class of an object by statically typing it in source code using the class name. Classes are particular kinds of objects, and the class name can serve as a type name. See “Class Types” (page 26) and “Enabling Static Behavior” (page 91).

Objects

2010-07-13 | © 2010 Apple Inc. All Rights Reserved.

CHAPTER 1

Objects, Classes, and Messaging

Memory Management

In any program, it is important to ensure that objects are deallocated when they are no longer needed—otherwise your application’s memory footprint becomes larger than necessary. It is also important to ensure that you do not deallocate objects while they’re still being used.

Objective-C offers two environments for memory management that allow you to meet these goals:

  • Reference counting, where you are ultimately responsible for determining the lifetime of objects. Reference counting is described in Memory Management Programming Guide.

  • Garbage collection, where you pass responsibility for determining the lifetime of objects to an automatic “collector.” 

    Garbage collection is described in Garbage Collection Programming Guide. (Not available on iOS—you cannot access this document through the iOS Dev Center.) 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章