- Next, simulate real-life situations in OC and create a car. First you have to have a car class, and then use the car class to create a car object
- Classes that define OC and objects that create OC
- To describe a class in OC is a little cumbersome, in 2 steps: The declaration of the class, the implementation of the class (definition). Similar to functions, functions have a sub-declaration and definition
- Declaration of the class
- Code writing
- Define a car class with 2 properties: Number of wheels, speed, 1 behaviors: Running
- Naming rules for class names \ Properties: Rules for identifiers
- Naming conventions for class names: Meaningful, hump-marked, first-letter capitalization
Declaration of #import <Foundation/Foundation.h>// class @interface car:nsobject{ @public int // How many wheels? int // Speed }-(void// run behavior @end
- Member variables
- Variables declared in @interface curly braces {}: Wheels, Speed
- @interface curly braces and curly braces for functions are not the same
- Default is initialized to 0
- @public
@public allows the wheels and speed properties of the car object to be accessed by the outside world
- NSObject
Plus: The purpose of NSObject is to have the car class capable of creating objects
- class the implementation
// implementation of the class @implementation car-(void) run{ NSLog (@ "%i wheels,%i the speed of the car ran up. " , wheels, Speed);} @end
- Creating objects
- Code writing
// Main function int Main () { // Create car object new]; C3; C; [C Run]; return 0 ;}
- Code analysis, memory analysis for the main function (objects have members in memory)
- [Car New] Creates a new object each time, and returns the address of the object, then the address of the object should be saved with a pointer variable
New];
Use a pointer variable C to point to the in-Memory car object
- Set the properties of a car object
As with pointers to structs, access struct properties,
3 ; c ;
- Create multiple Car objects
- Set only wheels, speed properties, respectively
New ];c14new];c2; [ C1 run];
- 1 assign to another, and then modify the property
New ];c14; C1*c2 = c1;c23; [ C1 run];
- The benefits of object-oriented encapsulation
- Closer to the way people think
- Just focus on the object and don't need to follow the steps
- object and Function parameters
- Object member variables as function arguments
- Pointer to object as function parameter
- To modify a pointer to a member of an object
- To modify the pointer's pointing
- Declaration and implementation of a class
- @interface and the @implementation Division of Labor
- @interface's like exposing the outside clock surface
- @implementation is like a structure hidden inside the clock.
- Declaring and defining multiple classes
- Common errors
- Only the declaration of the class, no implementation of the class
- Missed the @end.
- @interface and @implementation nesting
- Declaration nesting of two classes
- Member variables are not written in parentheses
- The declaration of the method is written in curly braces.
- Syntax details
- Member variables cannot be initialized in {} and cannot be accessed directly
- Method cannot be called as a function
- member variable \ Method cannot be decorated with keywords such as static, do not mix with C language (temporarily ignore)
- The implementation of the class can be written after the main function, mainly after the declaration.
- The difference between OC methods and functions
- The OC method can only be declared between @interface and @end and can only be implemented between @implementation and @end. This means that the OC method cannot exist independently of the class
- C functions do not belong to the class, and the class is not associated with the C function only to define the function of the file all
- C functions cannot access members of OC objects
- Low-level Error: The method has a declaration, but the implementation of the time is written as a function
- OC the method of attention
- Method only declared, not implemented (classic error)
- method is not declared, only implemented (compiler warning, but can invoke, OC's weak syntax)
- Compile time: Access no member variable directly error, Access no method, just warning
- @ Implementation
- There is no @interface, only @implementation, is also able to successfully define a class of
@implementation car:nsobject{ @public int// number of wheels int// speed }-(void) run{ NSLog (@ "%i wheels,% I ran at the speed of the car ", Wheels; @end
- The same member variable cannot be declared in the @implementation as @interface
- Method
Design a Caculator calculator class, which has the function of calculation (behavior)
- Methods with no parameters
- Design a method to return pi
// Method declaration -(double) pi; // method Implementation -(double) pi{ return3.14;}
- 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;}
- A method with multiple parameters
- Design a method of calculation and
// Method declaration -(double) sumOfNum1: (Double) num1 andNum2: (double)num2; // method Implementation -(double) sumOfNum1: (double) num1 andNum2: (Double)num2{ return num1 + num2;}
- Method Name Note
- The colon is also part of the method name
- Two object methods with the same name are not allowed in the same class
- Exercises
Car class design a method to compare the speed with other cars, if the car speed, return 1, if the car is slow, return 1, the same speed return 0
- Anonymous objects
- Property access
[Car New ;
[[Car New] run];
Object-oriented syntax-02