"OC" encapsulation, "oc 」

Source: Internet
Author: User

"OC" encapsulation, "oc 」
1. object-oriented and Encapsulation

Three main features of object-oriented: encapsulation, inheritance, and Polymorphism

In the OC language, @ interface and @ implementation are used to process classes.

@ Interface is like a clock surface exposed to the outside, like external display and interface. @ Implementation is like hiding the internal construction implementation of the clock and encapsulating the specific implementation.

  Ii. set Method

In the development process, considering the security requirements, we generally do not use @ public, @ protected, or other keywords before the member variable name, but use the Set method to provide the value of the member variable for the object. Some unreasonable values can also be filtered and filtered within the set method.

The role of the set Method: provides a method for setting the value of member variables.

Naming rules:

(1) The method name must start with set.

(2) Keep up with the name of the member variable after the set, with uppercase letters

(3) the return value must be void.

(4) be sure to receive a parameter, and the parameter type must be consistent with that of the member variable.

(5) The name of the parameter cannot be the same as the variable name of the member. (The variable name of the member recommended by Apple should be prefixed)

Benefits of the Set Method:

(1) Prevent data from being exposed, ensuring data security

(2) filter the Set Data

3. get Method

Get method: returns the member variables in the object for the caller.

Naming rules:

(1) There must be a return value. The type of the returned value is the same as that of the member variable.

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

(3) No parameters need to be received

Iv. Example: method declaration:
1 # import <Foundation/Foundation. h> 2 @ interface Student: NSObject 3 {4 // do not use @ public 5 // @ public 6 int _ age; 7 8 // read-only (readonly ): only allow external access to no, and do not allow external modification to no. 9 // @ public10 int _ no; // you only need to provide the get method 11} 12 13-(void) setAge: (int) newAge; 14 15-(int) age; 16 17 // learn 18-(void) study; 19 20 @ end
Method implementation:
# Import "Student. h "@ implementation Student // implement the set Method-(void) setAge: (int) newAge {// filter the passed parameters if (newAge <= 0) {newAge = 1 ;}_ age = newAge ;}// get method-(int) age {return _ age ;}- (void) study {NSLog (@ "% d-year-old student is learning", age) ;}@ end
Main function:
1 # import <Foundation/Foundation. h> 2 # import "Student. h "3 4 int main () 5 {6 Student * stu = [Student new]; 7 8 // stu-> age = 10; 9 10 [stu setAge: 10]; 11 12 [stu study]; 13 14 NSLog (@ "Student's age is % d", [stu _ age]); 15 16 return 0; 17}
Note 1:

In actual development, the set and get methods are not always provided. If the internal member variables such as the student's student ID are only allowed to be read, but cannot be modified, generally, only the get method is provided instead of the set method.

NOTE 2:

The name of a member variable starts with an underscore. The get method name does not need to be underlined. There are two advantages: (1) distinguish it from the method name of the get method; (2) some other local variables can be separated. Variables starting with an underscore are usually member variables of the class.

V. Category Methods

Methods that can be directly executed using class names (Classes occupy storage space in the memory, including the class \ object method List)

Comparison between class methods and object methods: (1) object Methods
  • Start with minus sign-
  • This method can only be called by objects without objects.
  • Object methods can access instance variables (member variables)
(2) class methods
  • Starting with a plus sign +
  • It can only be called by class name, and the object cannot be called
  • Instance variables (member variables) cannot be accessed in class methods)
  • Usage scenarios: When you do not need to access member variables, use the class method whenever possible.
(3) class methods and object methods can have the same name. 6. self keywords

Self is a pointer. If the current method is called, self points

[When it appears in the object method, it indicates the current object. When it appears in the class method, it indicates the current class]

Self usage:

(1) You can use the self-> member variable name to access the member variables in the current object (only in the object method)

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

7. encapsulate exercises

Design a score class

Code:

1/* 2 4. design a score Class 3 * C language score (readable and writable) 4 * OC score (readable and writable) 5 * Total score (read-only) 6 * average score (read-only) 7 */8 # import <Foundation/Foundation. h> 9 10 @ interface Score: NSObject11 {12 int _ cScore; // C language Score 13 int _ ocScore; // OC Score 14 15 int _ totalScore; // total score 16 int _ averageScoe; // average score 17} 18 19-(void) setCScore :( int) cScore; 20-(int) cScore; 21 22-(void) setOcScore :( int) ocScore; 23-(int) ocScore; 24 25-(int) totalScore; 26-(int) averageScore; 27 28 @ end29 30 @ implementation Score31-(void) setCScore :( int) cScore32 {33 _ cScore = cScore; 34 35 // calculate the total score 36 _ totalScore = _ cScore + _ ocScore; 37 _ averageScoe = _ totalScore/2; 38} 39-(int) cScore40 {41 return _ cScore; 42} 43 44-(void) setOcScore :( int) ocScore45 {46 _ ocScore = ocScore; 47 48 // calculate the total score of 49 _ totalScore = _ cScore + _ ocScore; 50 _ averageScoe = _ totalScore/2; 51} 52 // listener member variable changes 53 54-(int) ocScore55 {56 return _ ocScore; 57} 58 59-(int) totalScore60 {61 return _ totalScore; 62} 63-(int) averageScore64 {65 return _ averageScoe; 66} 67 @ end68 69 70 int main () 71 {72 Score * s = [Score new]; 73 74 [s setCScore: 90]; 75 [s setOcScore: 100]; 76 77 [s setCScore: 80]; 78 79 80 int a = [s totalScore]; 81 82 NSLog (@ "total score: % d", a); 83 84 return 0; 85}

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.