Objective-c instance method Visibility, method

Source: Internet
Author: User

One instance method visibility, Method 1. Visibility visibility of instance variables                           &NBSP ;                          ,         &NB Sp       features public (common)                         &NB Sp                     instance variables can be manipulated outside and inside the class  protected (protected, default)   &NB Sp                     instance variables can only operate within the class and its subclasses   private (private)   &NBS P                          ,         &NB Sp       Instance objects can only be accessed within that class note: the so-called internal, refers to the corresponding class of @implementation and @end   thinking: Why not use the @public keyword: 1. Use the @ Public keyword, Exposes the inner details of the class. 2. One of the three major features that do not conform to the object-oriented language __ package    two. 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: Instance variables   Instance methods cannot be used in class methods: Object only, for example:-(void) sayh;2. [Person Sayhi]; there is no "student call Sayhi" in OC This expression uses the message sending mechanism in OC: [receiver   message]; Correct expression: Send Sayhi message to student object. A.stu Dent received the message, that is, the method sayh;b. Student find the Sayhi method and execute. 3. ":" Flag parameters, cannot be omitted. There must be a colon parameter. The writing format of the 4.setter,getter. Format. If an instance variable is int age, or an int _age; setter is written in the following format:-(void) Setage: (int) age; That is, the instance variable name (set+) in uppercase. The getter's writing format is as follows:-(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 underscore)   three. Relationship to instance variables both setter and getter internal operations are instance variables each instance variable requires a pair of setter and getter method four: Custom initialization Method-(ID) Init This initialization method can only set default values for instance variables and is not flexible.-(ID) Initwithname: Custom initialization method, define initialization method based on requirements .  Example:-(ID) Initwithname: ( NSString *) name sex: (NSString *) sex;-(ID) initwithname: (NSString *) name sex: (NSString *) sex{ _name = Name;_sex = Sex ; return self;  five.   #import   Import header file, that is, import the contents of the header file into the current class. #import Import the Custom class, # import<> import the header file in the class library. Functions similar to C # include in the language, but can prevent the header file from being repeatedly imported   prone to recurring import header file problems   Six: @ class tells the compiler that the string after the @ class is used as the class name, and the interface content of the class is not imported. Effectively avoid nested loop imports.   Summary: A. Instance variable has three common visibility:@public, @protected, @private [email protected] violates encapsulation features, is seldom used in object-oriented development, @protected default visibility, and access to instance variables, which can be used in itself and in subclasses; You can access instance variables by using the private class. The method is the core of OC, using the message mechanism: [Receive message]. " -"message is called by the object;" + "message is called by the class.

Objective-c instance method visibility, method

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.