In oC, only pointer variables can be used to indirectly manipulate objects.The key word used for class declaration is @ interface.
@ InterfaceClass Name @ end
@ End at the end indicates that the class declaration ends.
The key word used for class implementation is @ implementation.
@ ImplementationClass Name @ end
@ End needs to be added at the end to tell the Compiler class implementation is complete.
/*
Class Name: Car attribute: Number of tires, speed (speed) behavior: run * // write a complete function: Function Description and definition (Implementation) // write a complete Class: class declaration and implementation // 1. Class declaration //: The car inherits from the nsobject class, the purpose is to make the car class have the ability to create objects (this class is in the foundation framework) # import <Foundation/Foundation. h> @ interface car: nsobject {// used to declare object attributes (
The member variable (also known as instance variable) is initialized to 0 by default.)
@ Public // change the access level of all subsequent attributes to public, so that external pointers can indirectly access the member variables in the object.Int wheels; // Number of tires int speed; // speed} // method (behavior): method name, parameter, and return value (the method is also declared and implemented)
// As long as it is the OC object method, it must begin with a minus sign (-)
// Any data type in the OC method must be enclosed in parentheses ().
// Parentheses () in the OC method: Enclose the Data Type
-(Void) run;@ End // 2. Implement the class (that is, implement the write method) @ implementation car // implement the method (clarify the code in the method)-(void) run {nslog (@ "the car is running") ;}@ end int main (){//
If you want to execute some behaviors in OC, write a brackets [behavior performer behavior name].// Use the class to create an object // execute the new action of the car class to create a new object
// Defines a pointer Variable P (the * on the Left cannot be omitted ). P will point to car objects in the future.
// [Car new] a new object is created each time, and the new object itself (the address of the new object) is returned)
Car * P = [Car new];Car * P2 = [Car new]; P2-> wheels = 5; P2-> speed = 300;
// Assign a value to the wheels attribute of the object pointed to by P
P-> wheels = 4;P-> speed = 250; // send a run message to the object pointed to by P [p run]; nslog (@ "The car has % d wheels and the speed is: % dkm/H ", p-> wheels, p-> speed); Return 0;} person * P = [person new]; person * P2 = P; the value assignment between pointers transmits the address stored by pointers, which is equivalent to the reference transfer in C #.
Classes in OC also occupy space in memory (classes are loaded into memory only once before class is used to create objects)
In addition, each object in OC has an ISA pointer by default, pointing to the class of the object to be created.Common Errors are only the declaration of classes. No class implementation leaks the @ end @ interface and @ implementation nesting (the declarations and implementations of the two classes cannot be nested) the Declaration of the two classes nested member variables are not written in the brackets. The method declaration is uninstalled from the braces.
Member variables must be written in braces of the class, and cannot be duplicated or initialized, and static cannot be added before
Class Declaration must be placed before (before use), The implementation part can be placed behind the difference between methods and functions: Object methods are all started with a minus sign (-) the implementation of object methods can only be written in the declaration of object methods between @ implementation and @ end. The object methods between @ interface and @ end can only be called by objects. Object methods are classified. All functions of an object can be written in any location in the file (it cannot be written in the middle of @ interface and @ end in the declaration part of the class ), function all function calls in the file do not depend on the internal function of the object. You cannot directly access the member variables of an object through the member variables.
If there is a method declaration in the class declaration, but it is written as a function during implementation, it will be considered that this method is not implemented.
Method declaration and implementation:
// In the OC method, a parameter corresponds to a colon (something that can be written before the colon)-(INT) pingfang :( INT) num ;//
Method Name:
Pingfang: (the colon is also included in the method name)Two parameters-(INT) sum :( INT) num1 :( INT) num2; when called: int A = [jsq sum: 10: 5];
Recommended Syntax:
-(INT) sumwithnum1 :( INT) num1 andnum2 :( INT) num2; // Method Name: sumwithnum1: andnum2:
Int A = [jsq sumwithnum1: 10 andnum2: 20];Writing colons to multiple parameters is also part of the method name. In the same class, two object methods cannot have the same name. Method Name: method call: method declaration of multiple parameters: call of methods with multiple parameters: anonymous object:
Do not write code similar to anonymous objects (which may cause memory leakage ):
Only output results are required.
[Car new]-> speed = 300;
[[Car new] Run];
IOS document: Installation Method 1: Put the docset file in/xcode. APP/contents/developer/documentation/docsets path Installation Method 2: put it in a hard drive path/users/pcbeta/library/developer/shared/documentation/docsets. If there is anything wrong, please correct it !!
Objective-C: 03 _ object-oriented-class and Object