Dark Horse programmer 09-Super keyword

Source: Internet
Author: User

--- Java training, Android training, IOS training, and. Net training. We look forward to communicating with you! ---

Role of the Super keyword

1. directly call a method in the parent class

2. Whether super calls class methods or object methods depends on the Super environment:

Super is in the object method, so it will call the object method of the parent class

Super is in the class method, then the class method of the parent class will be called

3. Usage: Some behaviors of the parent class are reserved when the child class overrides the parent class method.

1 // zombie 2 @ interface zoombie: nsobject 3-(void) walk; 4 5 + (void) test; 6-(void) test; 7 8 @ end 9 10 @ implementation zoombie11-(void) Listen 12 {13 nslog (@ "move two steps forward ******"); 14} 15 16 + (void) test17 {18 nslog (@ "zoombie + test"); 19} 20 21-(void) test22 {23 nslog (@ "zoombie-test"); 24} 25 @ end26 27 // jump zombie 28 @ interface jumpzoombie: zoombie29 + (void) Haha; 30-(void) haha2; 31 @ end32 33 34 @ implementation jumpzoombie35 36 + (void) haha37 {38 [Super test]; 39} 40 41-(void) haha242 {43 [Super test]; 44} 45 46-(void) Route 47 {48 // double-hop 49 nslog (@ "double-Hop"); 50 51 // double-hop (directly call the Walk Method of the parent class) 52 [super walk]; 53 // nslog (@ "move two steps forward ----"); 54 55} 56 @ end57 58 int main () 59 {60 // [jumpzoombie Haha]; 61 jumpzoombie * JZ = [jumpzoombie new]; 62 63 [JZ haha2]; 64 65 return 0; 66}

Analysis Code:

(1) Considering that the actual jumping zombie will "Jump in two steps", we have rewritten the Walk Method of the parent class zoombie.

At this time, line 49th indicates "Two hops", and then let the jumping zombie "move two steps forward". We need to call the Walk Method of the parent class. At this time, we need to use the super keyword.

(2) In line 5 and 6, the parent class declares two test methods, one is the class method and the other is the object method. We need to test when super calls class methods and when to call object methods.

Therefore, in line 36-44, we declare two methods in the subclass jumpzoombie: class method and object method.

 

Then, call [jumpzoombie Haha] separately. The class name jumpzoombie calls the class method Haha, takes priority to search for this class, finds lines 36-39, and runs, [Super test].

The output result is: zoombie + test, indicating that super calls the class method at this time.

 

[JZ haha2]; the object JZ calls the object method hahaha2, first goes to the original search, finds lines 41-44, and runs, [Super test]

Output result: zoombie-test indicates that super calls the object method at this time.

 

 

Super is in the object method, so it will call the object method of the parent class

Super is in the class method, then the class method of the parent class will be called

 

Dark Horse programmer 09-Super keyword

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.