[Study Notes] [OC language] polymorphism, learning notes oc Polymorphism

Source: Internet
Author: User

[Study Notes] [OC language] polymorphism, learning notes oc Polymorphism

1. Basic concepts of Polymorphism
Multiple forms of a certain type of thing
OC object with Polymorphism

2. embodiment of Polymorphism
Person * p = [Student new];
P-> age = 100;
[P walk];
Assign a subclass object to the parent class pointer
Attributes and methods for accessing parent class pointers

3. Benefits of Polymorphism
Receiving parameters with the parent class saves code

4. Limitations of Polymorphism
You cannot access attributes of subclass (forced conversion can be considered)

5. Details of Polymorphism
Dynamic binding: the dynamic calling method is determined based on the object type at runtime.

6. Code

1 # import <Foundation/Foundation. h> 2 3/* 4 Polymorphism 5 1. no polymorphism 6 without inheritance 2. code embodiment: the pointer of the parent class points to the subclass object 7. benefit: If the function \ method parameter uses the parent class type, you can pass in the parent class and subclass object 8. limitations: 9 1> variables of the parent class cannot directly call the method unique to the subclass. You must convert it to a subclass type variable to directly call the unique method 10 */11 12 // Animal 13 @ interface Animal: NSObject 14-(void) eat of the subclass; 15 @ end 16 17 @ implementation Animal 18-(void) eat 19 {20 NSLog (@ "Animal-eat ----"); 21} 22 @ end 23 24 // Dog 25 @ interface Dog: Animal 26-(void) run; 27 @ end 28 29 @ implementation Dog 30-(void) run 31 {32 NSLog (@ "Dog --- run"); 33} 34-(void) eat 35 {36 NSLog (@ "Dog-eat ----"); 37} 38 @ end 39 40 // Cat 41 @ interface Cat: Animal 42 43 @ end 44 45 @ implementation Cat 46-(void) eat 47 {48 NSLog (@ "Cat-eat ----"); 49} 50 @ end 51 52 // This function is specially used to feed animation 53 // void feed (Dog * d) 54 // {55 // [d eat]; 56 //} 57 // 58 // void feed2 (Cat * c) 59 // {60 // [c eat]; 61 //} 62 // 63 64 // If the parameter uses the parent class type, you can pass in the parent class and subclass object 65 void feed (Animal *) 66 {67 [a eat]; 68} 69 70 int main () 71 {72 // NSString * d = [Cat new]; 73 // [d eat]; 74 75/* 76 Animal * aa = [Dog new]; 77 // limitations of polymorphism: variables of the parent class cannot be used to call subclass Methods 78 // [aa run]; 79 80 // convert aa to Dog * type variable 81 Dog * dd = (Dog *) aa; 82 83 [dd run]; 84 */85 86 // Dog * d = [Dog new]; 87 88 // [d run]; 89 90/* 91 Animal * aa = [Animal new]; 92 feed (aa); 93 94 Dog * dd = [Dog new]; 95 feed (dd); 96 97 Cat * cc = [Cat new]; 98 feed (cc ); 99 */100 101/* 102 // NSString * s = [Cat new]; 103 Animal * c = [Cat new]; 104 105 106 NSObject * n = [Dog new]; 107 NSObject * n2 = [Animal new]; 108 109 110 // various forms 111 // Dog * d = [Dog new]; // Dog type 112 113 // polymorphism: the parent class Pointer Points to the subclass object 114 Animal * a = [Dog new]; 115 116 // The actual image of the object 117 [a eat] will be detected when the method is called; 118 */119 return 0; 120}

 

 

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.