"oc" Package

Source: Internet
Author: User

I. Object-oriented and encapsulation

Three main object-oriented features: encapsulation, 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

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

Iv. Usage Example: declaration of the method:
1 #import <Foundation/Foundation.h> 2 @interface student:nsobject 3 {4     //Member variable try not to use @public 5     //@public 6     int _age; 7      8     //Read Only (readonly): Only allow outside access to no, no external modification is allowed. 9     //@public10     int _no;//only need to provide a Get method}12-(void) Setage: (int) newage;14-(int) age;16 17//Learning-(void) Study;19 @end
Implementation of the method:
#import "Student.h" @implementation the implementation of the student//set Method-(void) Setage: (int) newage{    //filter the passed in 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 = 9     [Stu setage:10];11     [stu study];13 NSLog     (@ " The age of the student is%d years old ", [Stu _age]);     return 0;17}
Note 1:

In the actual development, the set and get methods are not necessarily provided, if the internal member variables such as the student's school number such as the data is only allowed to read outside, but does 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 a 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) distinguish it from the method name of the Get method, (2) You can drink some other local variable, and the variable that starts with the underscore, usually the member variable of the class.

v. Types of methods

A method that can be executed directly with the class name (the class itself occupies storage space in memory with a list of Class \ Object methods)

Comparison of class methods and Object methods: (1) Object methods
    • Start with minus sign-
    • Only objects can be called, no object, and this method cannot be executed at all.
    • Object methods can access instance variables (member variables)
(2) class method
    • Start with a plus + +
    • Can only be called with the class name, the object cannot be called
    • Instance variables (member variables) cannot be accessed in a class method
    • Use cases: When you do not need to access member variables, try to use the class method
(3) Class methods and object methods can have the same name vi. Self keywords

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

vii. Encapsulation Exercises

Design a grade 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 (Read only) 7 */8 #import <foundation/foundation.h&gt ; 9 @interface score:nsobject11 {_cscore;//C language score int _ocscore;//OC results from int _totalscore; Total score: int _averagescoe; Average}18 (void) Setcscore: (int) cscore;20-(int) cscore;21-(void) Setocscore: (int) ocscore;23-(int) ocscore;24     -(int) totalscore;26-(int) averagescore;27 @end29 @implementation Score31-(void) Setcscore: (int) CScore32 {33  _cscore = cscore;34 35//Calculate total score _totalscore = _cscore + _ocscore;37 _averagescoe = _totalscore/2;38}39     -(int) CScore40 {_cscore;42}43-(void) Setocscore: (int) ocScore45 {_ocscore = ocscore;47 48 Calculate Total Score _totalscore = _cscore + _ocscore;50 _averagescoe = _totalscore/2;51}52//Monitor member variable change-(int) OCSC Ore55 {_ocscore;57}58-(int) TotalScore60 {The _totalscore;62}63-(int) averageScore64 {_averagescoe;66}67 @end68 score int main () *s = [Score new];73 [s setcscore:9] 0];75 [s setocscore:100];76] [s setcscore:80];78 + int a = [s totalscore];81] Nslo G (@ "Total:%d", a); 0;85 return}

"oc" Package

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.