I. OC Method
(1) object Methods
(1) The object method starts with "-", for example, "-(void) xx;
(2) object methods can only be called by objects
(3) Access the member variables of the current object in the object method
(4) Call format [Object Name object method name];
(5) Design a student or dog class and use the exercise object method.
Student statement:
Student implementation:
Dog declaration:
Dog implementation:
Main Program:
(2) class methods
(1) class methods start with +, for example, + (void) put;
(2) class methods can only be called by classes
(3) instance (member) variables cannot be accessed in class methods, because class methods and classes are called, and no storage space is created to store member variables in the class.
(4) Call format: [class name class method name];
(5) advantages and applicability of class methods:
The execution efficiency is higher because it does not depend on objects;
If the problem can be solved using the class method, try to use the class method;
Occasion: when the member variable is not needed inside the method, you can change it to a class method.
(6) Design a calculator class and practice the class method.
Calculator class declaration:
Implementation of the calculator class:
Main Program: Call the class method directly using the class name
Note 1: class methods and object methods can have the same name.
NOTE 2: class methods can be called in object methods.
(3) method name
(1) methods without Parameters
Statement:
Call:
(2) methods with Parameters
Statement:
Call:
Note: The colon is also part of the method name.
Ii. File Compilation
In work, different classes are usually put into different files. The declarations and implementations of each class are separated and the declarations are written in. h. The header file is written in the corresponding. in the M file, what is the class name and what is the prefix of the file name.
Assume that there are two classes: the person class and the dog class. The following five files are usually used:
(1) Declaration file of the person. h person class
(2) implementation file of the person. m person class
(3) Declaration file of the Dog. h Dog class
(4) implementation file of the Dog. m Dog class
(5) Main. m main function (program entry)
Use # import to include the corresponding header file in the implementation file of the main function and class.
Supplement: import has two functions: one is to completely copy the content of a file like include; the other is to automatically prevent duplicate copies of the file content (even if the file is contained multiple times, copy only one copy ).
When you use the command line to compile a link file. M files are compiled in a single file, and then all target files are linked. However, in xcode, all objects are stored. M files are all compiled links. If a duplicate definition error occurs, most of the problems should be caused by repeated inclusion or inclusion of the file content. M file.
The. m or. c file cannot be contained in the source file, whether using include or import, but only the Declaration can be put. Therefore, classes are usually split into two parts in OC: declaration and implementation.
Tip: this is also a reflection of programming ideas. H and. M files are completely independent. Only for better readability is required, the file names of the two files must be consistent. This also separates interfaces from implementations, this eliminates the need for callers to focus on specific implementation details.
Xcode writes a row to compile a row. It has a simple repair function. Red indicates an error message and a yellow warning. If a variable is declared in the program, but it is not used, a warning is generated. When debugging a program, if you find that there is no error on the entire page, but it will be an error when you run it, it must be a link error.