OC Foundation 4: Classes and methods

Source: Internet
Author: User

1, the Declaration of the Class (@interface) to be placed in its own name class.h file, and the definition of the class (@implementation) to be placed in the same name of the class.m file;

2, import local file and import System file format is different: Import local file to use double quotation marks, import system files to use < and >, as follows:

#import <Foundation/Foundation.h>

#import "Calss.h"

3, the program is divided into three parts: Class.h, CLASS.M and MAIN.M, wherein: Import system files in the Class.h part, CLASS.M and main.m just import class.h;

4. About @property Directive and @synthesize directive:

(1), can be used only @property instruction without @synthesize instructions, if not @synthesize instructions, the @property directive declaration of those instance variables will be automatically named as "_" start, preferably two are used;

(2), @property instruction in the interface part, @synthesize instruction in the implementation part;

(3), the data type following the @property instruction does not require parentheses , as follows:

@property int num;

(4), @synthesize instructions do not need to follow the data type , and with the @synthesize instruction, the implementation section does not need to use curly braces to define these variables;

(5), in the interface section with the @property Directive declaration content is as follows:

@property int xxx;

Equivalent:

@interface xclass:nsobject{

int xxx;

}

-(void) setxxx: (int) n;

-(int) xxx;

The equivalent declares the variable (int) xxx and declares two methods of setxxx and XXX. Note The Set method is preceded by a set,get method without a get. (so you can access xxx directly with xclass.xxx, the equivalent of calling the XXX method.) )

(6), in the implementation section with the @synthesize directive definition content as follows:

@synthesize xxx;

Equivalent:

@implementation Xclass

-(void) setxxx: (int) n{

xxx = n;

}

-(int) xxx{

return xxx;

}

Equivalent to the completion of two functions, if you do not use the @synthesize directive, the compiler will generate the following code:

@implementation Xclass {

int _xxx;

}

-(void) setxxx: (int) n{

_XXX = n;

}

-(int) xxx{

return _xxx;

}

(7), the so-called instance variable will be automatically named as "_" beginning, but for the @implementation part. In the @interface section and in the main function, the property does not begin with "_" either by accessing the attribute with the dot operator or by invoking the setter and getter methods with the square brackets. From the second part of (6) can be seen, setter and getter method involves the name of the property or XXX, only in the method to use the specific properties, is the _xxx. That is, the so-called "_" attributes, only in the absence of the definition of @synthesize , in the @implementation The method defined in the section requires access to a property that begins with "_". all other places are regarded as normal names.

(8), after using the @property directive and the @synthesize directive to define attributes, if accessed using the dot operator, you can access the set method and the Get method directly with Xclass.xxx, and in square brackets it must be [Xclass setxxx] and [ Xclass XXX];

5, about the dot operator and square brackets:

(1), the dot operator is generally used in the property, that is, to set or get the value of the variable, such as:

Aperson.sex;

(2), square brackets are generally used in methods, such as:

[Aperson Run];

(3), the two can be mixed, from the grammar is not wrong, but it is best not to mix;

6, about the method of multiple parameters:

(1), the declaration of the method is as follows:

-(void) Setto: (int) n over : (int) D;

Note that n is a formal parameter and over is the second parameter name;

(2), the use of methods such as the following example:

[AClass setto:1 Over:2];

Fill in the parameter values according to the format of the method declaration, one by one;

(3), about the method declaration in (1), in fact, the parameter name over can be omitted, into the following form:

-(void) Setto: (int) n: (int) D;

The same is allowed, but it makes the code easy to confuse, it is best not to use this style;

7, if a method to take an instance of a class as a parameter, then declare this method when you remember to use "*", as follows:

-(void) XXX: (class *) C ;

8. Instance variables and local variables:

(1), instance variables refer to variables declared in the interface section (or inherited from the parent class), and instance methods can directly access their instance variables;

(2), a variable that is scoped to the program block that defines it is called a local variable, such as a method's formal parameter.

9, about the static keyword:

(1), the variable declared with static is a static variable , and its default value is 0;

(2), the value of static variables can be changed , the role of static just let the value of this variable has been preserved;

(3), as follows, there is a code snippet as follows:

-(int) showpage{

...

static int pagecount = 0;

++pagecount;

return PageCount;

...

}

If this function is not static, then the return value of each call is 1, each time the method is called again, the value of the variable is reassigned; After using static, the return value of the first call is 1, the second is 2, and so on, the value of the variable will persist and accumulate;

(4), (3) The code snippet is used to calculate the number of pages, the PageCount is set as an instance variable and set as a local static variable meaning is different:

The set as instance variable calculates the number of pages of an instance, and if there are two instances of a and B, then the values of the PageCount of A and B are not necessarily the same;

is set to a local static variable, then the value of PageCount is equal to the sum of the pages of A and B, because A and B calls to this method will be summed up PageCount;

10, self keyword: equivalent to this keyword, you can directly invoke the current object;

OC Foundation 4: Classes and methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.