[Objective-C] 08-self keyword

Source: Internet
Author: User

Note: This Objective-C topic is a prelude to iOS development. It also helps programmers with experience in object-oriented language development to quickly get started with Objective-C. If you have no programming experience or are not interested in Objective-C or iOS development, ignore this. Before learning this topic, we recommend that you first study the C language topic.

OC has a self keyword, which is similar to this in Java and represents the caller of the current method. However, there is a difference between self and this.

1. this in Java can only be used in dynamic methods, and cannot be used in static methods. 1. Use this keyword in dynamic methods.
1 public class Student {2     private int age;3     public void setAge(int age) {4         this.age = age;5     }6 }

This is used in row 4th. this indicates a Student object that calls the setAge method.

 

2. If this keyword is used in a static method, an error is reported.

 

Ii. self in OC can be used in both dynamic and static methods. 1. self is used in dynamic methods.
 1 @implementation Student 2  3 - (void)test2 { 4  5 } 6  7 - (void)test3 { 8     [self test2]; 9 }10 11 @end

* Two dynamic methods are defined in rows 3rd and 8th.

* The self keyword is used in row 8th. Here self represents a Student object that calls the test3 method.

* The function of the 8th line of code is to call the dynamic method test2 of the Student object.

 

2. self is in the static method
 1 @implementation Student 2  3 + (void)test2 { 4  5 } 6  7 + (void)test3 { 8     [Student test2]; 9     10     [self test2];11 }12 13 @end

* Two static methods are defined in rows 3rd and 7th, and the + number before the method is clearly displayed.

* Let's look at the second row. Here we use self. The self keyword is used in the static method test3.

* In the beginning, self indicates the caller of the current method. The current method is test3. Who is the caller of test3? It must be the Student class, because test3 is a static method. Therefore, self represents the Student class.

* Therefore, the functions of rows 8th and 10th are the same. The static method test2 is called.

 

3. self Summary

1> in a dynamic method, self represents an "object"

2> in static methods, self represents "class"

3> keep one sentence in mind. self indicates the caller of the current method.

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.