Objective-C learning note _ can the instance variable be? Degree and Method
? What are the instance variables? Degrees
Visibility |
Features |
Public) |
Instance variables can be operated both outside and inside the class. |
Protected (protected, default) |
The instance variables can only be in this class and its? Class internal operations |
Private) |
Instance objects can only be accessed in this class |
The so-called internal refers to the relationship between the @ implementation and @ end of the corresponding class.
The sample code is as follows:
# Import
@ Interface Person: NSObject {@ public NSString * _ holobby;/** <Interest */@ protected NSString * _ name;/** <name */NSInteger _ age; /** <age */@ private NSString * _ sex;/** <Gender */} @ end
The call code for @ public instance variables is as follows:
Person * person = [[Person alloc] init]; person-> _ holobby = @ play volleyball to watch a movie; NSLog (@ interest: % @, person-> _ holobby );
@ Protected:
@ Private:
Note:
Enable? The @ public keyword exposes the details of the class.
Which of the following features does not meet the requirements of object-oriented language? -- Encapsulation.
Therefore, avoid declaring instance variables as public during actual development.
? ? Method Classification
In Objective-C, there are two methods: class method and instance method.
Class Method: only the class can be used, for example: + (id) alloc Note: instance variables cannot be used in class methods.
Instance method: only objects can be used, for example,-(void) sayHi
? Legal Statement
Method Name
Indicates the type of the deletion method. The return type, parameter type, parameter name, and space are displayed. For example:
replaceObjectAtIndex:withObject:
Class, the ":" parameter cannot be omitted. A colon must have a parameter.
Assignment and value methods (setter, accessor, setter, and getter)
- (void)setName:(NSString *)name;- (NSString *)name;- (void)setName:(NSString *)name{ _name = name;}- (NSString *)name{ return _name;}
Multi-parameter method
- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex;- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex{ self = [super init]; if (self) { _name = name; _sex = sex; } return self;}
Usage
[person sayHi]
In Objective-C, there is no such expression as "person calls sayHi.Use the message sending mechanism in Objective-C: [receiver message].
Correct description: Send the sayHi message to the person object.
The person receives the message, that is, the method sayHi; person finds the sayHi method and runs it. Three setter getter
In OC, the method for assigning values to instance variables is called setter ).
Read instance variable value? Method is called getter ).
The assignment and value methods we wrote earlier can be called setter and getter.
Setter and getter writing formats
OC specifies the writing formats of setter and getter. If an instance variable isint age;
Orint _age;
The writing format of setter is as follows:
- (void)setAge:(int)age;
That is, the instance variable name whose first letter is set + ignored in upper case.
The getter writing format is as follows:
- (int)age;
That is, the return value type and the instance variable type? To ,? The legal name is the same as the instance variable name (ignore the underline ).
Relationship with instance variables
Both the setter and getter operations are instance variables. Each instance variable requires a pair of setter and getter methods.
? Define Initialization Method
-(Id) init this initialization method can only set the default value for instance variables, not flexible.
-(Id) initWithName: customizes the initialization method as needed.
The sample code is as follows:
- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex;- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex{ self = [super init]; if (self) { _name = name; _sex = sex; } return self;}
# Import
Import header file: import the content in the header file to the current class.
# Import "" to import the custom class, # import <> import the header file in the class library.The function is similar to # include in C, but it canAvoid headers? Duplicate Import,It is prone to loop header file import problems.
@ Class
Tell the compiler to use the string after @ class as the class name without importing the class interface content. This effectively prevents nested loop imports.
Example: Student Class, instance variable: name, age, Teacher; Teacher Class, instance variable: name, age, Student.
@ Class:
# Import
@ Class Student;/* @ class mark Student as a class, without importing any instance variables and Methods */@ interface Teacher: NSObject {@ protected NSString * _ name; NSInteger _ age; student * _ student;/* Student */}-(void) setName :( NSString *) name;-(void) setAge :( NSInteger) age; -(NSInteger) age;-(void) setStudent :( Student *) student;-(id) initWithName :( NSString *) name age :( NSInteger) age; -(void) teacherInfo; @ end
# Import Teacher. h # import Student. h/* in. import the header file */@ implementation Teacher-(void) setName :( NSString *) name {_ name = name;}-(NSString *) name {return _ name ;} -(void) setAge :( NSInteger) age {_ age = age;}-(NSInteger) age {return _ age;}-(void) setStudent :( Student *) student {_ student = student;}-(Student *) student {return _ student;}-(id) initWithName :( NSString *) name age :( NSInteger) age {_ name = name; _ age = age; return self;}-(void) teacherInfo {NSLog (@ name: % @ age: % ld myTeacher: % @, _ name, _ age, [_ student name]);} @ end
#import
@class Teacher;@interface Student : NSObject{@protected NSString *_name; NSInteger _age; Teacher *_myTeacher;}- (void)setName:(NSString *)name;- (NSString *)name;- (void)setAge:(NSInteger)age;- (NSInteger)age;- (void)setMyTeacher:(Teacher *)myTeacher;- (Teacher *)myTeacher;- (void)studentInfo;@end
#import Student.h#import Teacher.h@implementation Student- (void)setName:(NSString *)name{ _name = name;}- (NSString *)name{ return _name;}- (void)setAge:(NSInteger)age{ _age = age;}- (NSInteger)age{ return _age;}- (void)setMyTeacher:(Teacher *)myTeacher{ _myTeacher = myTeacher;}- (Teacher *)myTeacher{ return _myTeacher;}- (void)studentInfo{ NSLog(@name: %@ age: %ld myTeacher: %@, _name, _age, [_myTeacher name]);}@end
# Pragma mark knowledge point 5 @ class usage/* function: Avoid loop import * // * Student class, instance variable: name, age, Teacher * Teacher class, instance variable: Name, age, student */# if 1/* Create a Teacher object */Teacher * teaOne = [Teacher alloc] init]; [teaOne setName: @ Tom]; [teaOne setAge: 40];/* Create a Student object */Student * stuOne = [[Student alloc] init]; [stuOne setName: @ Zhang San]; [stuOne setAge: 18]; [stuOne setMyTeacher: teaOne];/* Create the teaOne object first */[stuOne studentInfo]; [teaOne setStudent: stuOne]; [teaOne teacherInfo]; /* New Teacher object */Teacher * teaTwo = [[Teacher alloc] initWithName: @ Steve age: 50]; [teaTwo setStudent: stuOne]; [teaTwo teacherInfo]; # endif