Objective-C-point syntax

Source: Internet
Author: User
Preface

In Java, we can access the public member variables of an object through "Object Name. member variable name", which is called "Point Syntax ". For example:

1. define a common member variable Age in row 2nd of the student class.

1 public class Student {2     public int age;3 }

2. Then, in row 5th, the point syntax is used to directly assign values to the age value of the member variable of Stu.

1 public class Test {2 3     public static void main(String[] args) {4         Student stu = new Student();5         stu.age = 10;6     }7 8 }

Of course, the formal approach is to privatize member variables and allow the outside world to access member variables using the public get and set methods.

3. Many advanced languages use this syntax. In order to help programmers in other industries get started with OC quickly, OC also introduces the syntax, but its meaning is not the same as that of Java.

 

I. Traditional get and set methods

Let's take a look at the traditional get and set methods. Defines a student class and has a member variable Age and the corresponding get \ set method.

1. Student. h
 1 #import <Foundation/Foundation.h> 2  3 @interface Student : NSObject { 4     int age; 5 } 6  7 - (void)setAge:(int)newAge; 8 - (int)age; 9 10 @end

1> in row 4th, a member variable age is defined, which has the @ protected permission. Therefore, the variable cannot be directly accessed from outside.

2> the Set Method and get method of age variables are declared in rows 7th and 8 respectively.

 

2. Student. m
 1 #import "Student.h" 2  3 @implementation Student 4  5 - (void)setAge:(int)newAge { 6     age = newAge; 7 } 8  9 - (int)age {10     return age;11 }12 13 @end

1> the set method is implemented in row 5th.

2> The get method is implemented in row 9th.

 

3. Main. m

Put the defined student class in the main function.

1 # import <Foundation/Foundation. h> 2 # import "student. H "3 4 int main (INT argc, const char * argv []) 5 {6 @ autoreleasepool {7 student * Stu = [[STUDENT alloc] init]; 8 9 // set the value of age 10 [STU setage: 10]; 11 12 // retrieve the value of age 13 int age = [STU age]; 14 15 nslog (@ "Age is % I", age); 16 17 [STU release]; 18} 19 Return 0; 20}

1> include the student header file in two lines

2> Create a student object in row 7th and release the student object in row 17th.

3> call the Set Method in Row 3 to set the age value.

4> call the get method in Row 3 to obtain the age value.

5> output the age value in row 15th. The output result is as follows:

00:26:19. 002 point syntax [6164: 303] age is 10

This is the simple use of the traditional get method and Set Method of OC. For Beginners, this syntax is strange, because its method call is completed in square brackets. Therefore, OC finally introduces the dot syntax.

 

Ii. Use the dot syntax instead of the traditional get and set methods

The above demonstrates the simple usage of the OC traditional get \ set method, and then uses the dot syntax instead.

The code of the main function in Main. m can be changed:

1 int main (INT argc, const char * argv []) 2 {3 @ autoreleasepool {4 student * Stu = [[STUDENT alloc] init]; 5 6 // set the value of age 7 Stu. age = 10; // equivalent to [STU setage: 10]; 8 9 // get the value of age 10 int age = Stu. age; // equivalent to int age = [STU age]; 11 12 nslog (@ "Age is % I", age); 13 14 [STU release]; 15} 16 return 0; 17}

1. Pay attention to the 7th line code and replace the original [STU setage: 10] with Stu. Age = 10. The two statements are completely equivalent. That is, the Stu. Age here does not mean to directly access the member variable Age of the stu object, but the compiler automatically expands the code into [STU setage: 10] When Stu. Age = 10]

In addition, if you directly access the member variable, the OC should use the following syntax: Stu-> age, rather than Stu. Age.

 

2. Pay attention to the 10th line code and replace the original int age = [STU age] With int age = Stu. Age. The two statements are completely equivalent, Stu. age does not directly access the member variable Age of the stu object, but the compiler encounters int age = Stu. the code is automatically expanded to int age = [STU age]

 

3. Therefore, the meaning of the OC point syntax is completely different from that of Java. The essence of the OC point syntax is method calling rather than directly accessing member variables. As for whether the syntax indicates the get method or the set method, it depends on whether you set the value or get method (such as 10th lines of code ), the set value is the set method (for example, 7th lines of code ).

 

4. If you want to verify whether the syntax is a method call, there are many methods.

For example, you can add some print information to the Set Method and get method of student. M. If the program runs the program and outputs the print information, it indicates that the get method or set method is indeed called.

1 # import "student. H "2 3 @ implementation Student 4 5-(void) setage :( INT) newage {6 nslog (@" setage method called "); 7 age = newage; 8} 9 10-(INT) Age {11 nslog (@ "Age method called"); 12 Return age; 13} 14 15 @ end

 

Iii. Point syntax and self traps

1. in Java, the this keyword indicates the method caller. That is to say, this indicates who calls this method. Therefore, the set method is generally written as follows:

1 public void setAge(int newAge) {2     this.age = newAge;3 }

The value of newage indicates that the value of newage is assigned to the age variable of the method caller.

 

2. There is a self keyword in OC, which is similar to this keyword. After I finish this, someone may want to write the OC set method as follows:

1 - (void)setAge:(int)newAge {2     self.age = newAge;3 }

The self in row 2nd represents the object that currently calls setage: method. However, the 2nd lines of code are absolutely incorrect and will lead to an endless loop. As I have mentioned earlier, the essence of the OC point syntax is method calling, so the above Code is equivalent:

1 - (void)setAge:(int)newAge {2     [self setAge:newAge];3 }

Obviously, it will cause the setage method to be called cyclically, and the program will crash like this.

 

4. A small suggestion

If it is the first time you come into contact with the OC point syntax, you may really think that Stu. Age means to directly access the member variable Age of the stu object. In fact, it is partly because the member variable name of the student class defined here is called age. In order to better access the regional sharding syntax and member variables, the member variables we define generally start with underscore. For example, _ age.

1. Student. H. Pay attention to the 4th rows.

 1 #import <Foundation/Foundation.h> 2  3 @interface Student : NSObject { 4     int _age; 5 } 6  7 - (void)setAge:(int)newAge; 8 - (int)age; 9 10 @end

2. Student. M. Pay attention to rows 6th and 10th

 1 #import "Student.h" 2  3 @implementation Student 4  5 - (void)setAge:(int)newAge { 6     _age = newAge; 7 } 8  9 - (int)age {10     return _age;11 }12 13 @end

 

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.