OC Object-Oriented encapsulation

Source: Internet
Author: User

I. Object-oriented and encapsulation

Three main object-oriented features: encapsulation (member variable), inheritance, 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 the display and interface, @implementation as if hidden in the clock inside the construction of implementation, the specific implementation encapsulated.

Second, set method

In the development process, taking into account the security requirements, we generally do not use @public in front of the member variable name, @protected such as keyword decoration, but use the Set method to provide the object member variable value, within the Set method can also be some unreasonable assignment filter filtering.

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>set followed by the name of the member variable, capitalize the first letter

3> return value must be void

4> must receive a parameter, and the parameter type needs to be consistent with the type of the member variable

The 5> parameter name cannot be the same as the member variable name (Apple's official recommended member variable name before adding _ to differentiate)

The benefits of the Set method:

1> data security by keeping data from being exposed

2> filtering the set of data

Example of using the Set method:

Declaration of the Set method:

#import<Foundation/Foundation>@interface  person:nsobject{    //  The @public keyword is no longer used here     nsinteger _age;} -(void) Setage: (Nsinteger) age; -(void) print; @end

Implementation of Set method

#import " Person.h " @implementation  Person // implementation of the Set method -(void) Setage: (nsinteger) age{    _age= Age;}
-(void) print{
NSLog (@ "Age=%ld", _age);
}@end

Test program

#import <Foundation/Foundation>#import"Person.h"int Main () {    *person=[[Person alloc] init];     // Setting the Person class object using the Set method the value of the age variable is ten    [Person Setage:ten]; return 0 ;}

Third, Get method

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

Naming conventions:

1> must have a return value, the type of the return value is the same as the type of the member variable

2> the same method name as the member variable name

3> does not need to receive any parameters

Example of using the Get method:

Declaration of the Get method

#import<Foundation/Foundation>@interface  person:nsobject{    * _name;} -(void) SetName: (nsstring) name; -(NSString *) name; @end

Implementation of the Get method

#import " Person.h " @implementation  Person // implementation of the Set method -(void) Setage: (nsinteger) age{    _name=name;} -(void) name{    return  _name;} @end

Test program

  #import  <foundation/foundation># Import   person   " int   main () {person  *person=< Span style= "color: #000000;"    >[[person alloc] init];  //  [ Person Setname:@ "  Zhang San   " "; NSLog ( @ " %@   ,[person name]);  return  0  ;}  

  Note :

1> in real-world development, the set and get methods are not necessarily provided, if the internal member variables such as student numbers such as the data value allows the outside to read, but does not allow the modification of the situation, usually only provide a GET method and do not provide a set method

The 2> member variable name begins with an underscore, the Get method name does not need to be underlined, and the underscore begins with two benefits: one is distinguished from the Get method name, the other is distinguished from some other local variables, and the variable that begins with the underscore is usually the member variable of the class.

Iv. Self keyword

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

2> appears in the object method, which represents the current object, and appears in the class method, representing the current class

Use of 3>self:

You can use the self or self-> member variable name to access member variables inside the current object (in Object methods only)

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

Exercises 

Requirements: Design a score class, OC results, C scores, can achieve total score, the average function

  

OC Object-Oriented encapsulation

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.