OC Study First day

Source: Internet
Author: User

1. Introduction to OC

OC is an object-oriented computer language.

OC is a C language based on the addition of a layer of the smallest object-oriented syntax, fully compatible with the C language.

2. OC Learning Goals

-Grammar Learning

-building object-oriented thinking ability

-Establish basic project requirement analysis capability

3. Object Oriented thinking

Object-oriented is a way to understand and abstract the real world, focusing on what objects are needed to solve the problem, encapsulating the function into objects, and emphasizing the objects with functions.

4. Classes and objects

Classes in OC are equivalent to drawings that describe a class of things. The class is then used to create the object, which is the concrete existence of the class.

Therefore, the object-oriented solution should be to consider which classes need to be designed, and how many objects are created using the class.

4.1 How to write the existence of a class

Start with @interface, end with @end, and then write the name of the object in the same class name, that is, the name of the category.

The first character of the class name must be uppercase.

The purpose of declaring a class is to tell the system what properties and behaviors we have in this class.

The properties in the OC class declaration can only be written in {} between @interface and @end.

Note: When writing the OC Class property, it is recommended that all properties be preceded by the name _.

After the class name: NSObject is designed to give our iphone class the ability to create objects.

By default, the properties in the OC object are not directly accessible, and the attribute is preceded by a @public to expose the properties in the class, and you can manipulate the properties in the object directly through a pointer to the struct body.

4.2 How to write a class implementation

Start with @implementation, end with @end, and then write the name of the class declared when the class corresponds, exactly as the class name you declared.

4.3 How to create an object from a class

Invoking a method with an object is a message mechanism in OC.

Format of the calling method in OC: [Class name or object name new]. (New is the method name)

[Class name or object name new] does three things: it opens up a new storage space in heap memory; Initializes a member variable (a property written in a class declaration brace is called a member variable, also known as an instance variable); Returns a pointer address.

4.4 Points of attention to objects

Members in an object can be accessed through objects, such as Object members (note that the declaring property is: @public).

Each object has a property of its own.

A member of one of the objects has been modified to have no relation to another object.

5. Declaration and implementation of object methods

5.1 Declaration of object methods

Format:

Characteristics:

The object method starts with a-(void) xx.

Object methods can only be called by objects.

The member variables of the current object can be accessed in the object method.
Call format [Object Name object method name].

5.2 Implementation of Object methods

Must be written at the beginning of the @implementation, between @end.
The implementation is represented by the addition of {} after the declaration.
The code that needs to be implemented is written in {}.

6. Declaration and Implementation of class methods

Declaration of a Class 6.1 method

Format:

Change Object Method-number to + number

Characteristics:

Save memory space.
Does not depend on the object, the execution is more efficient.
Use the class method as much as possible to solve the problem by using the class method.

Scenarios where the class method is applied:

You can change to a class method when you do not need to use a member variable inside a method.
Class methods are typically used to write tool methods.

Implementation of Class 6.2 methods: Same as the same method.

7. Storage details for objects

7.1 Storage Details for objects

class to create objects, each object occupies a certain amount of storage space in memory, each object has a separate member variable belonging to its own, all of the object common class member methods, methods in the whole memory only one copy, the class itself in memory occupies a portion of storage space, the method of the class is stored here.

7.2 Isa pointer

Each object contains an Isa pointer that points to the class to which the current object belongs.
ISA is a hidden pointer in an object that points to the class that created the object.
With the ISA pointer we can know at run time that the current object belongs to that class.

8. Comparison of functions and methods

Object method:
The implementation of an object method can only be written in @[email protected], and the declaration of an object method can only be written in the middle of @[email protected].
Object methods begin with a-number, and class methods begin with A + sign.
Object methods can only be called by objects, and class methods can only be called by classes and cannot be called as functions.
Functions belong to the entire file, can be written anywhere in the file, including @[email protected], but written in @[email protected] will not be recognized, the function can be declared inside the main function can also be outside the main function.
Object method collation \ Object all.
Function:
All of the functions are parallel.
The function does not have a membership relationship.
Can be called directly when used.
You cannot access member variables in an object.

9. Common errors

@interface @end and @implementation @end cannot be nested.

OC is weak syntax, can only @implementation, but in actual development do not do so.

Miss Write @end.
Object declaration order of two classes (can be scrambled in order).
The member variable is not written in {}.
The declaration of the method is written in {}.
You cannot initialize member variables of a class at declaration time, note that member variables cannot exist independently from the object.
Method cannot be called like a function.
Member variables and methods cannot be modified with keywords such as static, not to be confused with the C language.
The implementation of the class can be written behind the Mian function, as long as there is a declaration before use.

OC Study First day

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.