Objective-c Learning Notes _ instance variable can and method

Source: Internet
Author: User
Tags in degrees


? The instance variable is available in degrees.
Visibility of features
Public (Common) Instance variables can be manipulated outside and inside the class
Protected (protected, default) Instance variables can only operate inside this class and its? Class
Private (privately) Instance objects can only be accessed within this class


The so-called interior, refers to the corresponding class between @implementation and @end.



The sample code is as follows:

#import <Foundation/Foundation.h>

@interface Person: NSObject
{
    @public
    NSString *_hobby; /**< interest*/

    @protected
    NSString *_name; /**< name*/
    NSInteger _age; /**< age*/

    @private
    NSString *_sex; /**< gender*/
}
@end
The calling code of @public instance variable is as follows:

Person *person = [[Person alloc] init];
person->_hobby = @"Play volleyball and watch movies";
NSLog(@"interest: %@", person->_hobby);
The calling code of @protected instance variable is as follows:

The calling code of @private instance variable is as follows:

note:

Use the [email protected] keyword to expose the details of the class.

Doesn't meet the three characteristics of object-oriented languages?-Encapsulation.

Therefore, try to avoid declaring instance variables as public during actual development.

? ?Method Classification
There are two methods in Objective-C: class methods and instance methods.

Class method: Can only be used by class, for example: +(id)alloc Note: Instance variables cannot be used in class methods.

Instance method: can only be used by objects, for example: -(void)sayHi

?Legal Statement
Method name
Delete method type identification, return type, parameter type, parameter name, and spaces. E.g:

replaceObjectAtIndex:withObject:

A method with the same name cannot appear in a class. ":" identifies a parameter and cannot be omitted. A colon must have a parameter.

Assignment, value method (setter, accessor or setter, 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;
}
Method use
[person sayHi]

There is no such thing as "person calls sayHi" in Objective-C. Use the message sending mechanism in Objective-C: [receiver message].

Correct statement: send a sayHi message to the person object.

The person receives the message, the method sayHi;
The person finds the sayHi method and executes it.
Three setter getter
In OC, the method of assigning a value to an instance variable is called a setter.

The method of reading the value of an instance variable is called a getter (accessor).

The assignment and value methods we wrote before can all be called setters and getters.

Writing format of setter and getter
OC specifies the writing format of setter and getter. If an instance variable is int age; or int _age;

The writing format of setter is as follows:

-(void)setAge:(int)age; That is, set + ignoring the underscore and capitalized instance variable name.

The writing format of getter is as follows:

-(int)age; That is, the return value type is consistent with the instance variable type, and the method name is the same as the instance variable name (ignoring the underscore).

Relationship with instance variables
Regardless of whether the internal operations of the setter or the getter are instance variables, each instance variable needs a pair of setter and getter methods.

? Define the initialization method
-(id)init This initialization method can only set default values for instance variables, which is not flexible.

-(id)initWithName: Customize the initialization method, define the initialization method according to requirements.

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 the header file, that is, import the content in the header file to the current class.

#import “” Import custom classes, #import <> Import header files in the class library. The function is similar to #include in C language, but it can avoid header files being imported repeatedly, but it is prone to the problem of circular import header files.

@class
Tell the compiler that the string after @class is used as the class name, and the interface content of the class is not imported. Effectively avoid nested loop import.

Example: Student class, instance variables: name, age, teacher; Teacher class, instance variables: name, age, student.

The sample code used by @class is as follows:

#import <Foundation/Foundation.h>
@class Student; /* @class marks 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;
-(NSString *)name;

-(void)setAge:(NSInteger)age;
-(NSInteger)age;

-(void)setStudent:(Student *)student;
-(Student *)student;

-(id)initWithName:(NSString *)name
                 age:(NSInteger)age;

-(void)teacherInfo;
@end
#import "Teacher.h"
#import "Student.h" /* Import the header file in the .m 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 <Foundation/Foundation.h>
@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 use
/* Role: Avoid circular import */
/* Student class, instance variables: name, age, teacher
 * Teacher class, instance variables: 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

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.