Video iOS 05-Three encapsulation of the big features

Source: Internet
Author: User

The main learning object-oriented three major features: encapsulation , inheritance and polymorphism in the encapsulation

1. Encapsulation1> Benefits

Reduced coupling ratio

Properties in a class that can be called repeatedly

Improve security , external can not arbitrarily modify the value of variables, to ensure the security of the data

2> Set method

1. Function: Provide a method to set the member variables of the outside world, can be in the method to face the parameters of the corresponding filter

2. Naming conventions

1> method name must begin with set

2> set followed by the name of the member variable , the first letter of the member variable must be capitalized

3> return value must be void

4> must receive a parameter, and the parameter type is consistent with the member variable type

The name of the 5> parameter cannot be the same as the member variable name

3> Get Method

1. Function: returns the member variable inside the object

2. Naming conventions

1> must have a return value , and the return type must be consistent with the member variable type

2> the method name is the member variable name minus The underscore _ , the member variable is defined, usually begins with an underscore _

3> does not need to receive any parameters

4> member Variables

Naming conventions: Be sure to start with the underscore _

Role:
1. Make the member variable and get method separate
2. Can be distinguished from Local variables , a variable that sees the beginning of an underscore, is usually a member variable

5> Object methods and class methods1. Object Methods

1> minus - start

2> can only be called by an object

access to member variables (instance variables) in the 3> object method

2. Class Methods

1> plus + start

2> can only be called by Class (name)

Member variables (instance variables) cannot be accessed in the 3> class method

3. Benefits of class methods

1> is not dependent on the object, the execution efficiency is high

2> can use class method as far as possible

3> occasions: You can change to a class method when you do not need to use a member variable inside a method

Allow class methods and object methods to have the same name

Sample code

1#import <Foundation/Foundation.h>2 3 //Define a gender enumeration4typedefenum {5 Sexman,6 Sexwoman7 } Sex;8 9 @interface Student:nsobjectTen { One     //member Variables A     int_no; - Sex _sex; - } the //Statement of the set and get methods of the sex -- (void) Setsex: (Sex) sex; --(Sex) sex; - @end +  - @implementation Student + //implementation of the set and get methods for sex A- (void) Setsex: (Sex) Sex { at_sex =sex; - } --(Sex) Sex { -     return_sex; - } - @end in  - intMain () { to     //Create a Student object +Student *stu = [StudentNew]; -     //call the Set method of the Stu object's gender the [Stu Setsex:sexman]; *     //get method to call Stu object gender $NSLog (@"%d", [stu Sex]); Panax Notoginseng      -     return 0; the}

Video iOS 05-Three encapsulation of the big features

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.