Objective-c language-Object-oriented-encapsulation

Source: Internet
Author: User



First. Object-oriented and encapsulation



Three main object-oriented features: encapsulation (member variables), inheritance, and polymorphism



In the OC language, classes are processed using @interface and @implementation.






@interface as if exposed to the outside of the clock surface, like the outside to provide display and interface. @implementation is like a constructed implementation hidden inside the clock, encapsulating the specific implementation.






Second, set method



In the development process, given the security requirements, we generally do not use the @public, @protected and other keyword decorations before the member variable name, but instead use the Set method to provide the value of the member variable for the object. Some unreasonable assignments can also be filtered within the Set method.



The role of the Set method: To provide a way for the outside world to set the value of member variables



Naming conventions:



(1) method name must begin with set



(2) The name of the member variable followed by the set, with the first letter capitalized



(3) The return value must be void



(4) Be sure to receive a parameter, and the parameter type must be the same as the type of the member variable



(5) Parameter name cannot drink member variable name (Apple's official recommended member variable name plus _ to differentiate)



The benefits of the Set method:



(1) Data security is ensured by keeping data from being exposed



(2) Filtering the set of data



Example of using the Set method:



Declaration of the Set method:






The implementation of the Set method:






Test procedure:






Third, Get method



The function of the Get method: Returns the member variable inside the object for the caller



Naming conventions:



(1) There must be a return value, the type of the return value is the same as the type of the member variable



(2) The method name is the same as the member variable name



(3) No need to receive any parameters



The Get method uses an example:



Declaration of the Get method:






Implementation of the Get method:






Test procedure:









Note 1: In real development, the set and get methods are not necessarily provided, if the internal member variables such as the student's school number such as data only allow the outside to read, but do not allow the modification of the situation, usually only provide a GET method and do not provide a set method.



Note 2: The name of the member variable is preceded by an underscore, the Get method name does not need to be underlined, and two benefits are preceded by an underscore: (1) distinguished from the method name of the Get method; (2) You can drink some other local variables, and variables that begin with an underscore are usually member variables of the class.



Fourth. Self keyword



Self is a pointer, who calls the current method, and self points to who



"appears in the object method, represents the current object, appears in the class method, represents the current class"



Use of self:



(1) member variables within the current object can be accessed using the self-> member variable name (only in Object methods)



(2) [self method name]; Other object methods or class methods can be called



Fifth, practice



Requirements: Design A grade class






Realize:






Test procedure:






Objective-c language-Object-oriented-encapsulation


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.