This summary summarizes the assignment method of member variables in OC----settet, getter named method point operator1.settet, getter naming methodThe method of assigning a value to an instance variable in an object is called a modification method, which modifies the state of the object, which is called the setter. The other corresponding method of fetching is called the Getter naming habit-setter method is named after the variable name that is changed, such as the variable named age The Setage-getter method is named after the variable name that is obtained, such as the variable named Age, which is 2. The point operator named method Point operators cannot access member variables in OC class objects, such as student.age=5, which is not used as The setage, instead of calling the Member method, called the method [student Setage:5], which is equivalent to assigning a value to Student.age. NSLog (@ "%d", student.age); This is not the case with the member variable age, but rather the call to the member method, the value of the student.age expression, which is actually called the member method, Age,[student. “. ” You cannot call a member variable, just a shorthand for calling a set function and a Get function. It is not understood here that the get function and the SET function are defined to invoke member variables, because when you use Student.age, you will get an error when you write the two functions in a normal form.Create a student class to assign values to the class's Objects in the main function#import <Foundation/Foundation.h>
#import "Student.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {Student *stu = [[Student alloc] init];stu.name = @ "abc";//This is the dot operator[Stu setname:@ "Lao Wang"]; [Stu Setage:90]; NSString *name = [stu name]; NSLog (@ "Live next Door:%@", name); }
return 0;}. h file#import <Foundation/Foundation.h> @interface student:nsobject{
NSString *name;
int age;
}
//setter method, for assignment, format:-(void) Set variable name (first capitalization):(member variable type) parameter name-(void) SetName: (NSString *) newName;The //getter method is used to take a value, so a return value is required, this return value type is consistent with the member variable type, the format:-(member variable type) member variable name-(NSString *) name;-(void) Setage: (int) age1;-(int) age; @end. m file#import "Student.h" @implementation student-(void) SetName: (NSString *) newname{
name = NewName;} -(NSString *) name{
return name; -(void) Setage: (int) age1{
if (Age1 > | | Age1 < 0) {//Use the IF condition for a judgment
NSLog (@ "unreasonable age, please re-assign value");
}else{
age = Age1;
}}-(int) age{
return age;} @end finally say that the functions in the "+" and "-" OC before the common function names are often preceded by "+" and "-", where the plus minus represents a different method type. + functions that represent functions as classes, called with the class name. -functions that represent functions as objects are called when called. The class does not have space, there is no space for member variables, and the methods of the class cannot use the member variables of the object.
OC Summary Third speaking setter getter method dot operator