Encapsulation, inheritance, polymorphism, classification in OBJECTIVE-C

Source: Internet
Author: User

Benefits of Encapsulation:

    1. Filter for unreasonable values
    2. The assignment process inside the Shield
    3. Let the outside world not pay attention to the internal details

Benefits of Inheritance:

    1. Do not change the original model based on the method of extension charge

    2. Establish a connection between classes and classes

    3. The public code was extracted

    4. Cons: Strong coupling (when a parent class is removed, the subclass will no longer be used)

Points of note for inheritance:

    1. Subclasses and parent classes cannot have the same member variable
    2. Subclasses can override methods of the parent class
    3. Access procedure for subclass methods and properties: If the subclass does not have access to the parent class's

Inheritance and Composition:

1 @interfaceScore:nsobject2 {3         int_cscore;4         int_ocscore; 5 }6 @end7 8 @implementationscore9 @endTen  One @interfaceStudent:nsobject A { -Score *_socre;//There's a combination here, because you can't say that grades are a student . -        int_age;  the } - @end -  - @implementationStudent + @end

Combinations and inheritance can be understood as follows:

    • Inheritance is xxx is xxx
    • combination is xxx own xxx

Polymorphism : the ability of different objects to respond to the same name method in their own way is called polymorphic

polymorphism is simply saying: The parent pointer points to the subclass object

Benefits of Polymorphism:

    • Receive parameters with parent class, save code
#import<Foundation/Foundation.h>@interfaceAnimal:nsobject@end@implementationAnimal- (void) eat{NSLog (@"Animal----Eat food");}@end@interfaceDog:animal@end@implementationDog- (void) eat{NSLog (@"Dog----Eat food");}@end@interfaceCat:animal@end@implementationCat- (void) eat{NSLog (@"Cat----Eat food");}@end//the parent class type used in the parameter can pass in the child class, the parent class objectvoidFeed (Animal *a) {[A eat];}//This function embodies the benefits of polymorphism, saving codeintMain () {Animal*AA =[[Animal alloc] init];        Feed (AA); Dog*DD =[[Dog alloc] init];        Feed (DD); Cat*CC =[[Cat alloc] init]; Feed (cc);}

Limitations of polymorphism:

    • A variable of the parent class type cannot directly invoke a specific method of the subclass (cast is used)
// Forced Conversions Person *p = [[Student alloc] init]; // If the study is a student-specific method, if you want to call a forced conversion // OC is weak syntax if [P study] can also (is dynamic bound), but because the compiler will appear warning, so do not write with a cast to make it more reasonable Student *s = (Student *) p;[ s study];
    • Dynamic binding: Determines the method of dynamic invocation at run time based on the type of object

Category-category:

the role of classification: You can add some methods to the class without changing the original class content (for collaborative development)

Use of classification Note:

    1. Categories can only increment methods and cannot increase member variables
    2. Member variables in the classification method implementation that can access declarations in the original class
    3. Classification can re-implement methods in the original class, but overwrites the methods in the original class, causing the original method not to be used (so the methods in the original class are not reproduced in the taxonomy)
    4. Priority of Method Invocation: Classification (the last category that participates in the compilation takes precedence)---> Original class---> Parent class

Encapsulation, inheritance, polymorphism, classification in OBJECTIVE-C

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.