OC Basics and Object Basics

Source: Internet
Author: User
Tags access properties define null

Class: Allows us to customize a new data type, which is a combination of several small variables and behaviors.

The nature of a class: A data type that is customized. But the class itself is just a new type of data that is created and cannot be directly used

Property: Is the object that is created when you create it, and the property belongs to the object.

1. Memory storage and storage of classes in memory

1. Five large areas in memory

2. Class loading: When we create an object, we definitely need to access the class, because only the class is accessed to know which members are in the class.

Even if a class pointer is declared, the class is accessed to determine whether the class exists

When the class is first accessed, the class is stored in the code snippet,—— class loads

This process will not have a property declaration, just a simple code load (of course it is not so simple)

Duration of the process: from loading to the end of the program

2. Object declaration in-Memory stored procedure

1. Declaration of the object

Lhperson *p1; Suppose you already have a Lhperson class that inherits from NSObject

2. The above statement simply declares a pointer variable, and the type of the pointer variable is the Lhperson type.

3. P1 is a local variable, so the P1 pointer variable is stored in the stack area

4. [Lhperson new] This part is the real object to create

First, request a suitable size space in the heap memory; [Lhperson Alloc]

Then, in the application of the space inside the template based on the class to create objects;

Class, these properties are declared in this object one by one.

There is another property,-isa, which is a pointer to the class in which the code snippet belongs

5. Initialize the properties of the object and assign a default value to the object's properties

If it is a basic data type, the default is 0;

If it is a C pointer type, the default is null;

If the OC pointer type, the default is nil;

6. Returns the address of this object in the heap space

Assign this address to the P1 pointer of the stack area

At this point, P1 points to the Lhperson object of the heap area.

3. Storage and structure of methods and classes

1. Only the properties of the class in the object are added with an Isa pointer, there is no method

2. Through the pointer, find the object pointed to by this pointer → through this pointer to find the properties of the object

3. Through pointer variables, the objects in the heap are found, and the methods in the classes in the code snippet are found through the ISA pointer in the object

4. Why a method does not exist in an object: Because no matter how many objects there are, the code of the method is the same, there is no need to save multiple copies

4. NULL

1. is a value, the value of a pointer variable in the C language

2. If the value of a pointer is null, it means that the pointer does not point to any space in memory

3. Null is essentially a macro → #define NULL ((void *) 0);

5. Nil

1. Also a value, also a pointer variable value

2. Essentially a macro,

#define _DARWIN_NULL ((void *) 0);

So, as you can see from here, nil and NULL are basically equivalent.

6. Nil and NULL

1. Null to the C pointer

2. Nil to OC Pointer

7. If a lhperson *p1 = nil;

At this time, it does not point to any object,

1. Unable to access properties via P1

2. Although the calling method does not make an error, the method does not execute.

OC class pointer variables of the same type can be assigned to each other

1. Lhperson *P1 = [Lhperson new];

Lhperson *p2 = p1;

At this time, p1 and P2 point to the same object.

Access the object either through the P1 pointer or through the P2 pointer, accessing the same object

OC Multi-file Development general Practice:

1. A class is a module, a module of at least two files

. h file header file, declaration file

. m file Implementation file

Object as a parameter of a method

1. Types of parameters

-(void) method name: (Declared class name *) parameter name

2. When calling this method, the argument object must match the formal parameter requirements

3. Object passing is address delivery

After passing, the formal parameter pointer and the argument pointer actually point to the same object.

Inside the method, the object is actually accessed by accessing the object through the parameter pointer.

Object as the return value of the method

1. Must be a return value

The return value should be a class pointer type

-(Lhperson) Buyperson;

After the method execution is completed,

2. NOTE!!!

Because objects are stored in the heap, objects persist even after the method has ended

3. Essentially, a class pointer is returned, that is, the address of the newly created object in the method

Properties of the class

1. The properties of a class represent what this class has.

2. Analyze the properties of a class? (common) What is there, what is it

Methods of the class

1. The class method represents the behavior that this class has, and the functionality that this class has

Object as a property of a class--nested struct body

1. The nature of the attribute is the variable

When creating an object, the attributes in the object are created in accordance with the rules in the last class.

What type of property is in the class template, and what type is the property in the object?

If the object's property is another object, this property is just a pointer variable, and there is no object generation, at this time, also to assign a value to this property an object's address, in order to use the normal

2. Class A can be used as an attribute of Class B, which means B has a

property is just a variable, and at this point in B, the possession of a is only a pointer variable, and no new object is created.

So if you want to use this at this time, you should also assign an object to the A property of the B object.

OC Basics and Object Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.