Benefits of Encapsulation:
- Filter for unreasonable values
- The assignment process inside the Shield
- Let the outside world not pay attention to the internal details
Benefits of Inheritance:
Do not change the original model based on the method of extension charge
Establish a connection between classes and classes
The public code was extracted
Cons: Strong coupling (when a parent class is removed, the subclass will no longer be used)
Points of note for inheritance:
- Subclasses and parent classes cannot have the same member variable
- Subclasses can override methods of the parent class
- 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:
- Categories can only increment methods and cannot increase member variables
- Member variables in the classification method implementation that can access declarations in the original class
- 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)
- 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