Ios basics (4) OC objects and ios basic oc objects
1. Create an OC class
Write a complete function: the Declaration and definition of the function are required.
Write a complete Class: class declaration and implementation are required.
1. Class Declaration
Declares the attributes and behaviors of an object.
1 # import <Foundation/Foundation. h> 2 3 @ interface Car: NSObject 4 {5 @ public 6 int speed; // speed 7 int wheels; // wheel 8} 9 10-(void) initSpeed :( int) speed withWheels :( int) wheels; 11 12-(void) run; 13 14 @ end
Row 3rd: NSObject aims to enable the Car class to create objects.
Row 5th: @ public allows external pointers to indirectly access member variables in the object.
Row 6 and Line 7: Define the attributes of an object.
Row 10th: defines an action with an input parameter.
Row 3: defines the behavior of an object. As long as it is the OC object method, it must begin with minus sign.
Any data type in the return value of the OC method must be expanded with parentheses.
Implement the method declared in @ interface.
2. Class implementation
The implementation of the Car class.
1 # import "Car. h" 2 3 @ implementation Car 4 5-(void) run {6 NSLog (@ "The Car ran up. Speed is % d km/h, number of wheels is % d ", speed, wheels); 7} 8 9-(void) initSpeed :( int) speed withWheels :( int) wheels {10 self-> speed = speed; 11 self-> wheels = wheels; 12} 13 14 @ end
Line 1 code: assign an initial value to the attribute speed and wheels of an object. Attributes cannot be initialized in the class declaration @ interface.
In xcode, functions are automatically implemented:
Enter "-", enter the first few letters of the function, and select the method automatically popped up by xcode.
3. Class call
1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3 4 int main(int argc, const char * argv[]) { 5 Car *car = [Car new]; 6 car -> wheels = 4; 7 car -> speed = 100; 8 [car run]; 9 10 [car initSpeed:200 withWheels:5];11 [car run];12 13 14 return 0;15 }
Row 3: In OC, if you want to execute some actions, you can connect a brackets [behavior performer behavior name]. Use classes to create objects.
[Car new] creates a new object and returns the new object itself (the address of the new object ).
Row 6 and Line 7: assign a value to the wheels and speed attributes of the object to which the car points.
Row 8th: Send a run message to the object to which the car points.
Example 1 of object and function parameters
1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3 4 void test1(Car *newC){ 5 newC -> wheels = 5; 6 } 7 8 9 int main(int argc, const char * argv[]) {10 Car *car = [Car new];11 car -> wheels = 4;12 car -> speed = 100;13 [car run];14 15 test1(car );16 [car run];17 18 return 0;19 }
The returned result of Line 1 code is:The car ran up. Speed is100 km/h,The number of wheels is5
The memory diagram is as follows:
Example 2
1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3 4 void test2(Car *newC){ 5 Car *c2 = [Car new]; 6 c2 -> wheels = 5; 7 c2 -> speed =300; 8 9 newC = c2;10 newC -> wheels = 7;11 }12 13 int main(int argc, const char * argv[]) {14 Car *car = [Car new];15 car -> wheels = 4;16 car -> speed = 100;17 [car run];18 19 test2( car );20 [car run];21 22 return 0;23 }
The returned result of the 20th line of code isThe car ran up. Speed is 100 km/h,The number of wheels is 4.
The memory diagram is as follows:
Benefits of object-oriented encapsulation:
1) closer to human thinking.
2) You only need to pay attention to objects and do not need to follow the steps.