Objective-c source Code (v) The realization principle of associated Objects

Source: Internet
Author: User
One instance method visibility, method
1. Visibility of instance variables
Visibility Features
public instance variables can be manipulated outside and inside the class
 
protected (default, default) instance variables can only be manipulated within this class and its subclasses
 
 private (private) instance objects can only be accessed within this class
Note: The so-called internal refers to the relationship between @implementation and @end of the corresponding class
 
Thinking: why not use the @public keyword:
1.Use the @public keyword to expose details inside the class.
2. Does not conform to one of the three major characteristics of object-oriented languages __ package
 
 
There are two methods in .OC: class method and instance method
1.Class methods: can only be used by classes, for example: + (id) alloc Note: You cannot use instance variables in class methods
 
Instance method: can only be used by objects, for example:-(void) sayH;
2. [person sayHi];
There is no such expression "student calls sayHi" in OC
Use message sending mechanism in OC: [receiver message];
Correct expression: send sayHi message to student object.
a.student received the message, namely the method sayH;
b. student finds the sayHi method and executes it.
3. ":" indicates the parameter, can not be omitted. There must be a parameter with a colon.
4.Setter, getter writing format.
The writing format of setter and getter is specified in OC.
If an instance variable is int age; or int _age;
 
The format of the setter is as follows:-(void) setAge: (int) age; i.e. the set instance variable name
(Ignore the underscore).
The format of the getter is as follows:-(int) age; that is, the return value type is the same as the variable type, and the method name is the same as the instance variable name (underline is ignored)
 
Relationship with instance variables
Both setters and getters operate on instance variables.
Each instance variable requires a pair of setter and getter methods
Four: custom initialization method
-(id) init This initialization method can only set default values for instance variables, which is not flexible.
-(id) initWithName: Custom initialization method, define initialization method according to requirements.
 
Example:
-(id) initWithName: (NSString *) name sex: (NSString *) sex;
-(id) initWithName: (NSString *) name sex: (NSString *) sex {
 
_name = name;
_sex = sex;
return self;
 
}
V. #import
Import header file, that is, import the contents of the header file into the current class.
#import "" import custom classes, # import <> import header files in class library.
The function is similar to #include in C, but it can prevent the header file from being imported repeatedly.
 
Prone to cyclic import header file problems
 
Six: @class
Tells the compiler that the string after @class is used as the class name, and the interface content of the class is not imported.
Effectively avoid nested loop imports.
 
to sum up:
a. There are three common visibility of instance variables: @public, @protected, @private
b. @public violates the encapsulation feature and is rarely used in object-oriented development; @protected has default visibility and can be used in itself and subclasses-> to access instance variables; @ private can be used in its own class-> to access instance variables.
c. The method is the core of OC and adopts a message mechanism: [receive message].
"-" Message is called by the object;
The "+" message is called by the class.
Objective-C instance method visibility, method

label:

Original address: http://www.cnblogs.com/zzu-xll/p/4964462.html
Related Article

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.