I. 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
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
Examples of Use:
Declaration of the method:
1 #import<Foundation/Foundation.h>2 @interfaceStudent:nsobject3 {4 //member variables try not to use @public5 //@public6 int_age;7 8 //Read Only (readonly): Only allow outside access to no, no external modification is allowed. 9 //@publicTen int_no;//only need to provide a Get method One } A -- (void) Setage: (int) NewAge; - the- (int) age; - - //Learning -- (void) study; + - @end
Implementation of the method:
#import "Student.h"@implementationStudent//implementation of 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 (@"the%d-year-old student is studying", age);}@end
Main function:
1 #import<Foundation/Foundation.h>2 #import "Student.h"3 4 intMain ()5 {6Student *stu = [StudentNew];7 8 //stu->age = ten;9 Ten[Stu Setage:Ten]; One A [Stu Study]; - -NSLog (@"The age of the student is%d years", [Stu _age]); the - return 0; -}
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 the underscore is preceded by two benefits: (1) distinguished from the method name of the Get method; (2) You can drink some other local variable, and the variable that starts with the underscore is usually a member variable of the class.
Iv. 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
five. 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
Six, packaging practice
Design a grade class
Code:
1 /*2 4. Design a score category3 * C language score (readable and writable)4 * OC result (readable and writable)5 * Total score (read only)6 * Average score (read only)7 */8 #import<Foundation/Foundation.h>9 Ten @interfaceScore:nsobject One { A int_cscore;//C Language scores - int_ocscore;//OC Results - the int_totalscore;//Total - int_averagescoe;//Average score - } - +- (void) Setcscore: (int) Cscore; -- (int) Cscore; + A- (void) Setocscore: (int) Ocscore; at- (int) Ocscore; - -- (int) Totalscore; -- (int) Averagescore; - - @end in - @implementationscore to- (void) Setcscore: (int) Cscore + { -_cscore =Cscore; the * //Calculate Total Score $_totalscore = _cscore +_ocscore;Panax Notoginseng_averagescoe = _totalscore/2; - } the- (int) Cscore + { A return_cscore; the } + -- (void) Setocscore: (int) Ocscore $ { $_ocscore =Ocscore; - - //Calculate Total Score the_totalscore = _cscore +_ocscore; -_averagescoe = _totalscore/2;Wuyi } the //change of listener member variable - Wu- (int) Ocscore - { About return_ocscore; $ } - -- (int) Totalscore - { A return_totalscore; + } the- (int) Averagescore - { $ return_averagescoe; the } the @end the the - intMain () in { theScore *s = [scoreNew]; the About[s Setcscore: -]; the[s Setocscore: -]; the the[s Setcscore: the]; + - the intA =[s totalscore];Bayi theNSLog (@"Total scores:%d", a); the - return 0; -}
iOS Development "oc"03-Encapsulation