OC Summary Third speaking setter getter method dot operator

Source: Internet
Author: User

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

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.