Objective-C method visibility, method,

Source: Internet
Author: User

Objective-C method visibility, method,
Method 1: visibility of an instance method. method 1. visibility of instance variables attributes public instance variables can operate protected externally and internally (protected, default) instance variables can only operate private (private) instance objects in the class and its subclass. Note: Internal, it refers to thinking between @ implementation and @ end of the corresponding class: Why not use the @ public Keyword: 1. the @ public keyword is used to expose the internal details of the class. 2. does not meet one of the three characteristics of object-oriented language _ encapsulation 2. there are two methods in OC: class method and instance method 1. class Method: can only be used by class, for example: + (id) alloc Note: The instance variable instance method cannot be used in class Methods: can only be used by objects, for example:-(void) sayH; 2. [person sayHi]; In OC, there is no "student calls sayHi". In OC, the message sending mechanism is used: [handler er message]. The correct statement is: Send a sayHi message to the student object. a. student receives the message, that is, the method sayH; B. student find the sayHi method and execute. 3. ":" parameter cannot be omitted. A colon must have parameters. 4. the writing format of setter and getter. the writing formats of setter and getter are specified in OC. if an instance variable is int age; or int _ age; the format of setter is as follows:-(void) setAge :( int) age; that is, the instance variable name with the upper Letter of the set + letter (ignore the underline ). getter is written in the following format:-(int) age; that is, the return value type is the same as the variable type. The method name is the same as the instance variable name (ignore the underline. relationship with instance variables whether it is setter or getter internal operations are instance variables every instance variable requires a pair of setter and getter Methods 4: Custom initialization method-(id) the init initialization method can only set the default value for instance variables, but is not flexible. -(id) initWithName: Custom initialization method, which is defined as needed. example:-(id) initWithName :( NSString *) name sex :( NSString *) sex;-(id) initWithName :( NSString *) name sex :( NSString *) sex {_ name = name; _ sex = sex; return self;} 5. # import the header file, that is, import the content in the header file to the current class. # import "" to import the custom class, # import <> import the header file in the class library. the function is similar to the # include function in C, but it can avoid the problem of loop import header files when the header files are repeatedly imported. 6: @ class tells the compiler to use the string after @ class as the class name, the interface content of the class is not imported. this effectively prevents nested loop imports. conclusion:. instance variables have three common visibility types: @ public, @ protected, and @ privateb. @ public violates the encapsulation feature and is rarely used in object-oriented development; @ protected default visibility, which can be used by itself and its subclass-> access instance variables; @ private can be used in the class itself-> access instance variables. c. the method is the core of OC and uses the message mechanism: [receive message]. "-" message is called by objects; "+" message is called by classes.

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.