OC for iOS Development (review notes)-Classes and objects (1)

Source: Internet
Author: User

The theory is boring, so it's a little long. Ha haha ~

To learn a language, we must first understand it, then

"What is OC"

Objectiv-c, abbreviated to OC, is an object-oriented language written on the basis of C language. fully compatible with C language, can be mixed with C language code in OC code, even C + + code;

You can use OC to develop applications for Mac OS x platforms and iOS platforms.

(a) First of all, first understandProcess orientedAndObject Oriented:

"Process oriented"

1, process-oriented language all statements are imperative

2, process-oriented language is the mapping of mathematical logic

3, process-oriented Language program = data + algorithm

C language is a process-oriented language, to solve the problem is the process of attention .

"Object Oriented"

1, the object as a unit of receiving messages, different objects are responsible for receiving and processing an aspect of the data, different objects cooperate to complete the function

2, is the life logic mapping

3. Program = object + Message

OC is an object-oriented language, and the problem is focused on the object .

As for "which method is better"--- Concrete analysis of specific circumstances

Tips:

> Object-Oriented language development steps

1. According to the requirements of the design class

2. Create the desired object using the class

One OC program written--run

>

/* method one :

1. Writing OC Program:. M source file ( create OC program in terminal touch xxx.m )

2. compile the. m file for the. o target file: cc-c xxxx.m

3. link. o file for a.out executable: cc xxxx.o-framework Foundation

4. execute the a.out file:./a.out

One step

CC Xxxx.m-framework Foundation

method Two : Of course you can also use Xcode (hey, not so hard to force)

*/

(ii) primary knowledge of classes and objects

1. Recognize

class: A property that has an object, which is a description of the object.

Object: Instantiation of class, visible and palpable

The highest realm of martial arts is called "All Things are objects ".

2.

"From the angle of the package"

The "struct" is the data Encapsulation of the structure

"function" for processing data. Encapsulation of the algorithm

A "class" encapsulates the data structure to be processed and the algorithm that processes the data .

 class = struct + function

" analysis from the perspective of programming language "

"Class" It is a custom type that does not occupy memory

"Object" A variable created by a class that uses a type-defined variable consumes memory

3. Steps to resolve OC issues ( core idea )

First: Find the object you need in the title

Second: List the member variables and member methods of the object

Third: Create the appropriate class based on the object found

IV: Creating Objects with classes

Five: Send the object message, let the object complete the corresponding function

4. 3 steps to design a class:

1). class name

2). Properties

3). Behavior (Action \ Function)

(iii) Classes and objects---combat class = attribute data Encapsulation (member variable) + method (function) encapsulation

1. "References to header Files"

#import <Foundation/Foundation.h>

#import "Cube.h"

<> represents the header file of the import system

"" means importing a custom header file

tip : #import function is consistent with #include and prevents the contents of the header file from being included multiple times

contains the NSObjCRuntime.h within the foundation framework

The main header file for the foundation framework should be included Foundation.h

2. class declaration and implementation

2. 1, "Declaration of class"

code example

@interfacedog:nsobject{//property data encapsulated within {}//member variable type member variable name;        int_age;//Attribute member name plus _ ( member variable )    float_weight;}//Encapsulation Method//the most common method setter (set)/getter (GET)//C function Syntax: Return value type function name (parameter list)//OC Method Syntax://method Type//-Method of the object//+ Methods of the class//method Type (return value type) Partial method name: (parameter type) parameter name 1 part variable name: (parameter type) parameter name 2;//Multiple Parameters//the Setter/getter method is primarily designed to protect the data security of the object//Setter method for member variables-(void) Setage: (int) NewAge;//void setage (int age)-(void) Setweight: (float) W;//getter method for member variables-(int) Age;//the pair-(int) getage is optimized, and the method name corresponds to the member variable-(float) weight;//Functional Methods-(void) run;-(void) bark;@end

Code description

A.

" first line code "

@interface class Name: parent class name

1) declare a class with a keyword @interface

2)interface followed by the class name

3) The colon represents the inheritance relationship followed by the parent class

For example , you create a dog class.

( : NSObject --Let the dog class have the ability to create objects)

b.

"Member variable"

member variables (instance variables) are declared between curly braces, the member variable is what is in the Description object .

C.

"Member Methods"

1) non-parametric member method with no return value

-(void) method name;

2) with parameters

-(void) method name: (parameter type) parameter;

3) method with multiple parameters

-(void) Label 1: (Type of parameter 1) parameter and label 2: (Type of parameter 2) formal parameter;

4) with return-worthy method

-(return value type) method name;

"A member function is a description of what an object can do"

3, "Implementation of the class"---can also be said to be the implementation of the method

 @implementationDog//Implementing class Method keyword class name// Implement the method here (that is, write the specific code)//implementation of Set method-(void) Setage: (int) newage{_age=NewAge;} //implementation of the Get method-(int) age{return_age;} //Functional Methods-(void) Run {NSLog (@"run ... .."); }  @end

4. "Object creation and use"

1) Create an object

Class name * pointer variable name =[class name alloc/new];

A new object is created using the Alloc/new method of the class (a space is declared in the heap space) and the address of the object is returned to the left pointer variable

(so you need to use a pointer variable of that class type to receive it).

Dog * dog = [dog alloc];

2) Working with objects

1> Accessing member Variables

Pointer variable name , member variable name = specific value ;

C->speed = 100; (If you write @public before the member variable )

2> Let objects Execute Methods

[Pointer variable name method name ];

[C Run];

[pointer variable name part method name : Argument 1 part method name : Argument 2];

[C SumOfNum1:andNum2: $];

message mechanism : sends a message to the object to which the pointer variable is pointing (the message name is the method name), and the object executes the appropriate method (dynamic)

intMain () {//referencing the actual object by defining a class pointer variable//class Instantiation Object Alloc, requesting space on the heapDog* Wangcai =[Dog alloc]; [Wangcai setage:5];//call The Set method to set the property[Wangcai setweight: -]; intA = [Wangcai age];//call the Get method to get the value       floatW =[Wangcai weight]; //to send a message to an object [object method], or "Object message"//sends a run message to the object that the pointer variable Wangcai points to//The object that receives the message, called the message receiver[Wangcai Run];                [Wangcai bark]; NSLog (@"A =%d W =%f", a,w); return 0;} 

5. "Method Invocation"

No parameter no return value format: [Object/class Name message/method/function name]

No return value has parameters: [Object/class Name message/method/function name: parameter]

a method call with a return value returns a variable of a value type = [Object/class Name message/method/function name]

/*

The difference between OC object methods and functions

1. The function belongs to the entire file, can be called anywhere in the file; object methods belong only to objects, only objects can call object methods

2. object methods can only be declared between @interface and @end , and object methods can only be implemented between @implementation and @end .

The Declaration and definition of a function can be written anywhere , the function cannot be owned by a class, only belongs to a file

*/

Add:

How objects are stored in memory

1. Each object has its own member variable, and the member variables of each object are independent and unaffected

2. There is a list of methods within each object, the code of the method is the same

OC for iOS Development (review notes)-Classes and objects (1)

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.