1. Writing the OC Program:. m source File
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
//design (definition) of a car class//@implementation and @end//: nsobject: Let car this class has the ability to create objects (inheritance)@implementationCar:nsobject//This curly brace can only write all the attributes.{ //@public: Allows the object's properties to be accessed by the outside pointer, which is private by default @public intWheels//the initial value of the default base data type is 0}//in front of @end, curly braces {} write behavior outside//add a behavior to a Car object (method)//To add a behavior to an object, you must start with a minus sign, adding an object to the class is +. //the parentheses () in the OC method are just the main extension types- (void) run{//To access the properties inside the car object, you can use the property name directlyNSLog (@"%i a wheel,%f the speed of the car run up! ", wheels, Speed);}+(Void) test{//This is the class method. The above-number is the object method. }@end
// in OC, to perform some behavior, first write a [behavior performer's behavior name] // New This behavior is completed, will return the address of this object new ]; // assign a value to the wheels property of the car object to which C points 4 // equivalent to (*c). Wheels = 4 // "Send" a run message to the object that the pointer variable C points to to let the object perform the run behavior [C Run];
- Declaration & Implementation
// declares a class 1. class Name 2. Inherited NSObject 3. Declaring a property 4. Declaring a method (just declaring, not implementing) 5. Member variables in the implementation and declaration cannot have the same name */ @interface book:nsobject{ Span style= "color: #0000ff;" > @public double Price; // declares a method (behavior) -(void @end
// define (implement) a class /* */@implementation-(void) reng{ NSLog (@ " %f's book was thrown away! ", Price);}
- Declaration and use of multiple parameters
- (void) fly{NSLog (@"I can fly, my age is%d", age);}//one parameter corresponds to a colon//the colon is also part of the method name- (void) Fly: (int) howheight{NSLog (@"I can fly, my age is%d, my height%d", age, Howheight);}//the case of multiple parameters. Withtime is part of the method name. Times is the parameter name- (void) Fly: (int) Howheight:withtime (int) times{}
New ]; [P fly]; [P Fly: About ]; [P Fly: Withtime: £ © ];
- The difference between OC object methods and functions
1. The function belongs to the entire file, can be called anywhere in the file, the object's method belongs to only this object, only the object can call
2. The method of the object can only be declared between @infterface and @end, and the object method can only be implemented between @implementation and @end.
The Declaration and definition of a function can be written anywhere, and the function cannot be owned by a class and belongs to only one file.
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)