I. object-oriented and process-oriented ideas
OC is object-oriented and C is process-oriented. Object-oriented and process-oriented are only two different ideas for solving problems.
Terms
- Process-oriented procedure oriented
- Object-oriented object oriented (oo for short)
- Object-Oriented Programming Object Oriented Programming (OOP)
Ii. Relationship between classes and objects
Object-oriented has two very important concepts: class and Object
Surface object in oC
1) classes in OC are equivalent to drawings used to describe a class of things. That is to say, to create an object, you must first have a class
2) OC uses classes to create objects. Objects exist in classes.
3) Therefore, the object-oriented solution should first consider which classes need to be designed and how many objects need to be created using classes.
3. Define OC classes and create OC objects
- It is a little troublesome to describe classes in OC. There are two major steps: class declaration and class implementation (Definition ). Similar to functions, functions are defined and declared separately.
1. Class Declaration
1) code writing
- Defines a car class and has two attributes: Number of wheels, speed, and 1 Action: Run
- Naming rules of class name/attribute: Rules of the identifier
- Naming rules for class names: meaningful, camper ID, uppercase letters
1. Class Design:
1> Class Name
* The first letter of the class name must be in uppercase.
* Underlines are not allowed.
* Multiple English words, identified by camper
2> attributes
3> behavior (function)
2. Plants vs. botnets
* Class Name: zoombie
* Attributes: life, speed, gongjili
* Behavior: Walk, bite, and die
3. Lightning planes
* Class Name: Plane
* Attributes: life, gongjili, speed, and bombcount
* Behavior: Fly, bomb, shoot, and die
4. Computer
* Class Name: Computer
* Attributes: band and expiredate
* Action: Open and Close
1 # import <Foundation/Foundation. h> 2 3 // class declaration 4 5 @ interface car: nsobject 6 7 {8 9 @ public10 11 int wheels; // how many wheels 12 13 int speed; // speed 14 15} 16 17-(void) run; // running behavior 18 19 @ end
2) member variables
- Variables declared in braces {} of @ interface: wheels, speed
- @ Interface braces are different from function braces.
- The default value is 0.
3) @ public
@ Public: the wheel and speed attributes of the car object can be accessed by the outside world.
4) nsobject
In addition, nsobject aims to enable the car class to create objects.
2. Class implementation
// Class implementation
1 @ implementation Car 2 3-(void) run 4 5 {6 7 nslog (@ "% I wheel, % I the speed of the car ran up", wheels, speed ); 8 9} 10 11 @ end
3. Create an object
1) code writing
1 // main function 2 3 int main () 4 5 {6 7 // create a car object 8 9 car * c = [Car new]; 10 11 C-> wheels = 3; 12 13 C-> speed = 300; 14 15 16 17 [C run]; 18 19 Return 0; 20 21}
2) code analysis and memory analysis of the main function (the object has members in the memory)
- [Car new] each time a new object is created and the address of the object is returned, the address of the object should be saved with a pointer variable.
Car *c = [Car new];
Point A pointer variable C to the car object in the memory.
- Set attributes of car objects
Similar to accessing the struct attributes with a pointer pointing to the struct, use->
c->wheels = 3;c->speed = 300;
4. Create multiple car objects
- Set only the wheels and speed attributes respectively.
1 Car *c1 = [Car new];2 3 c1->wheels = 4;4 5 Car *c2 = [Car new];6 7 c2->speed = 250;8 9 [c1 run];
- One value is assigned to the other, and then the attribute is modified.
1 Car *c1 = [Car new]; 2 3 c1->wheels = 4; 4 5 c1->speed = 250; 6 7 Car *c2 = c1; 8 9 c2->wheels = 3;10 11 [c1 run];
5. Benefits of object-oriented Encapsulation
- Closer to human thinking
- You only need to focus on objects and do not need to follow the steps
6. Objects and function parameters
- Object member variables as function parameters
- Pointer to object as function parameter
Modify a member pointing to an object
Modify pointer pointing
Iv. Class description and Implementation 1. division of labor between @ interface and @ implementation
* @ Interface is like exposing the clock surface to the outside
* @ Implementation is like constructing and implementing a clock.
2. Declare and define multiple classes
3. Common Errors
L only declarations of classes, no implementation of Classes
L missing @ end
L @ interface and @ implementation nesting
L declaration nesting of two classes
L member variables are not written in brackets
L The method declaration is written in braces
4. Syntax details
L member variables cannot be initialized in {} and cannot be directly retrieved for access.
L The method cannot be called as a function.
L member variables/methods cannot be modified with keywords such as static. Do not mix them with C language (temporarily ignore)
The implementation of class l can be written after the main function. It can be implemented after the declaration.
5. Differences between OC methods and functions
L The OC method can only be declared between @ interface and @ end, and can only be implemented between @ implementation and @ end. That is to say, the OC method cannot exist independently of the class.
L c functions do not belong to the class and are not associated with the class. C functions are only owned by the file defining the function.
L c functions cannot access members of OC objects
L low-level error: methods are declared, but functions are written during implementation.
6. OC Methods
L methods are declared and not implemented (Classic errors)
L The method is not declared and only implemented (compiler warning, but can be called, weak OC syntax)
L during compilation: an error is reported directly when no member variables are accessed. If no method is accessed, it is only a warning.
// As long as it is the OC object method, it must start with minus sign-
// Any data type in the OC method must be expanded with parentheses ()
// Parentheses () in the OC method: Enclose the Data Type
7. @ implementation
- Without @ interface, only @ implementation can be used to successfully define a class.
1 @ implementation car: nsobject 2 3 {4 5 @ public 6 7 int wheels; // how many wheels 8 9 int speed; // 10 11} 12 13-(void) run14 15 {16 17 nslog (@ "% I wheel, % I speed car ran up", wheels, speed); 18 19} 20 21 @ end
- @ Implementation cannot declare the same member variables as @ Interface
V. Methods
Method
1. All object methods are based on minus signs-
2. The object method declaration must be written between @ interface and @ end.
The implementation of object methods must be written between @ implementation and @ end.
3. Object methods can only be called by objects
4. Object method category \ object all
Function
1. functions can be written anywhere in the file (except between @ interface and @ end). functions belong to all files.
2. function calls do not depend on objects.
3. The function cannot directly access the member variables of an object through the member variable name.
Design a caculator calculator class, which has the computing function (behavior)
1. Methods without Parameters
* Design a method to return pi
1 // method declaration 2 3-(double) PI; 4 5 // method implementation 6 7-(double) PI 8 9 {10 11 return 3.14; 12 13}
* Method call
2. method with a parameter
* Design a square Calculation Method
1 // method declaration 2-(double) Square :( double) number; 3 // method implementation 4-(double) Square :( double) number5 {6 return number * number; 7}
* Method call
3. Methods with multiple parameters
* Design a calculation and Method
1 // method declaration 2-(double) sumofnum1 :( double) num1 andnum2 :( double) num2; 3 // method implementation 4-(double) sumofnum1 :( double) num1 andnum2 :( double) num25 {6 RETURN num1 + num2; 7}
* Method call
4. method name note
* The colon is also part of the method name.
* In the OC method, a parameter corresponds to a colon
* Two object methods cannot have the same name in the same class.
Vi. Anonymous objects
L attribute access
[Car new]->speed = 200;
L method call
[ [Car new] run];