OK, let's first explain what is called "object-oriented", in fact, is to find an object, and then facing her is the ~ ha, open a joke ~~~~
First, the basic concept
Object-oriented: the object-oriented design that we refer to here is generally referred to as object-oriented programming.
Object-Oriented Programming (English: object-oriented programming, abbreviation:OOP) is a programming paradigm and a method of program development. An object refers to an instance of a class. It takes the object as the basic unit of the program, encapsulates the program and data to improve the reusability, flexibility and extensibility of the software.
Object-oriented programming can be thought of as a kind of idea which contains various independent and mutually invoking objects in the program, which is opposite to the traditional idea: the traditional program design advocates treat the procedure as a set of functions, or a series of instructions to the computer directly. Each object in object-oriented programming should be able to accept data, process data, and communicate it to other objects, so they can all be thought of as a small "machine", or object .
Objective-c is an object-oriented computer programming language.
Two, object-oriented and process-oriented comparison
process-oriented : Handling transactions, mainly at the core of the process. To put elephants in refrigerators, for example, if you want to do this, you have to walk three steps:
1) Open the refrigerator
2) put the elephant in the refrigerator
3) close the refrigerator;
So, these three steps, we collectively refer to the process. Embodied in the computer programming language is the function-oriented, in the form of functions to solve the problem of programming, the use of thought is the process-oriented thinking . The famous C language is the process-oriented language .
Object-oriented : When dealing with a transaction using object-oriented thinking, it is not the process, but the object itself, if I want to do something, I should immediately think of what the object can do, let this object to do it for me, rather than to achieve it. For example, I would like to open the door, I will go to the "door" this object, because open door only "door" can Do, "opened" is an action, also "door" a method, and we just played the caller's identity, and do not care about the "door" is how "open", these are given to the object itself. The corresponding, in the programming language, is the class and the object. The traditional object-oriented languages are: Objective-c, C + +, Java, C # and so on.
Iii. relationship of classes and objects
A class is a collection (abstract) of a set of things that have the same (characteristic) attributes and behaviors (features). An object is a concrete implementation of a class. Everything is the object of the world. A class is actually a data type, and its variables are objects.
in OC, the definition of a class is divided into two parts: an interface (@interface) and an implementation (@implementation).
Interface section:
1) Declares the name of the class and the parent Class 2) declares the member variable 3) declares the Member Method 4) The file ends with a. h
For example
/* Class Name: Car properties: Number of tires, speed (velocity) behavior: Run *///because of the use of Nsobject#import <foundation/foundation.h>//1. class declaration//Declaration object Properties, behavior//: NSObject Purpose: Let Car this class has the ability to create objects @interface car:nsobject{//is used to declare object properties (instance variables \ member variables, default is initialized to 0) //@ Public can allow external pointers to indirectly access member variables inside the object @public int wheels;/////// speed (xxkm/h)}//Method (Behavior): Method name, parameter, return value (declaration, Implementation)//As long as the method of the OC object must be preceded by a minus sign-any data type in the start//OC method must be enclosed in parentheses () to enclose the parentheses in the//OC method (): Enclosing data type-(void) run; @end
Implementation section:
Contains the real implementation of the method, which is the concrete manifestation of the method. The implementation file ends with. M.
Note:. h files,. m files do not put in different folders, each of the. h files is a good habit to correspond to a. m file ~
For example:
2. Implementation of the class//used to implement the method declared in @inteface @implementation car//method implementation (say clearly what code in the method)-(void) run{ NSLog (@ "The car ran up");} @end
object creation and use:
Memory allocation: Allocates memory addresses dynamically for an object.
Initialize: The initial value that is filled in memory.
int main () { ///In OC, to perform some behavior, write a bracket [behavior performer behavior Name] //Use a class to create an object //Perform the new behavior of the car class to create a Novel object //define a pointer variable p, P Future object to car type //[car new] Creates a new object each time and returns the new object itself (the address of the new object) car *p = [car new]; Car *P2 = [car new]; P2->wheels = 5; P2->speed = +; [P2 run]; Assigns the wheels attribute of p to the object p->wheels = 4; P->speed = +; Send a run message to P pointing object [P run]; NSLog (@ "Car has%d wheels, speed level:%dkm/h", P->wheels, p2->speed); return 0;}
Iv. Discuss---method
1) method without parameters
Method declaration-(double) pi;//method implementation-(double) pi{ return 3.14;}
2) method with one parameter
? Design a method for calculating squares//Method declaration-(double) square: (double) number;//method implementation-(double) square: (double) number{ return number * number ;}
3) method with multiple parameters
? Design a method for calculation and/or method declaration-(double) SumOfNum1: (double) num1 andNum2: (Double) num2;//method implementation-(double) SumOfNum1: (double) NUM1 ANDNUM2: (double) num2{ return num1 + num2;}
Method Name Note: 1) A colon is also part of the method name 2) Two object methods with the same name are not allowed in the same class
V. Anonymous objects (as an understanding)
? property access [Car new]->speed = 200;? method call [[Car New] run];
It is not recommended to write in the code so that you can read it well.
The Dark Horse programmer------OC Object-oriented