"Objective-c" 08-self keyword

Source: Internet
Author: User


    • The This in Java can only be used in dynamic methods, not in static methods
    • The self in OC can be used either in dynamic methods or in static methods


Description: This objective-c topic is a prelude to learning iOS development, and to enable programmers with experience in object-oriented language development to quickly get started with objective-c. If you do not have programming experience, or are not interested in objective-c, iOS development, please ignore. before studying this topic, it is recommended to study the C language topic first.



There is a self keyword in OC, and the usage is similar to this in Java, which represents the caller of the current method. But self and this are still different.


Back to Top

1. Using the This keyword in a dynamic method

 
 
1 public class Student {
2     private int age;
3     public void setAge(int age) {
4         this.age = age;
5     }
6 }


This is used on line 4th, where this represents a student object that calls the Setage method





2. If the This keyword is used in a static method, the error is directly






Back to Topthe self in OC can be used either in dynamic methods or in static methods1.self in the dynamic method
 
1 @implementation Student
 2 
 3 - (void)test2 {
 4 
 5 }
 6 
 7 - (void)test3 {
 8     [self test2];
 9 }
10 
11 @end


* 2 dynamic methods defined in line 3rd and line 8th



* The Self keyword is used on line 8th, where self represents a student object that calls the Test3 method



* The function of the 8th line of code is to invoke the student object's dynamic method Test2





2.self in a 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


* 2 static methods defined in line 3rd and 7th , see the + number in front of the method



* Look at line 10th, where self is used. Clearly, the Self keyword was used in static method Test3.



* From the outset, selfrepresents the caller of the current method. The current method is Test3, who is the caller of the test3? This is definitely the student class, because Test3 is a static method. So the self here represents the student class .



* As a result, lines 8th and 10th are the same, and all are called static methods Test2





3.self Summary


1> in dynamic methods, self stands for "Object"



2> in a static method, self stands for "class"



3> original aim, remember a word: Selfrepresents the caller of the current method



"Objective-c" 08-self keyword


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.