What are the three main features of object-oriented?(
encapsulation, inheritance, polymorphism )Package:
- In the OC language, classes are processed using @interface and @implementation.
@interface as if exposed to the outside of the clock surface, to provide the external display and interface. @implementation is like a constructed implementation hidden inside the clock, encapsulating the specific implementation. Encapsulation is mainly embodied in: instance variable, object method, class method encapsulation
In the development process, given the security requirements, we generally do not use the @public, @protected and other keyword decorations before the member variable name, but instead use the Set method to provide the value of the member variable for the object. Some unreasonable assignments can also be filtered within the Set method. The function of the Set method: To provide a method naming specification for setting member variable values (1) The method name must be preceded by a set (2) set followed by the name of the member variable, the first letter capital (3) The return value must be void (4) must receive a parameter, and the parameter type needs to be consistent with the type of the member variable (5) parameter name cannot be the same as member variable name (Apple's official recommended member variable name to differentiate) Set method benefits: (1) Do not expose the data, Guaranteed Data Security (2) Filtering Set method Use Example: Set method declaration: Set method implementation: Test program:
- Get method
- The function of the Get method: Returns the member variable inside the object for the caller
- Naming conventions:
- (1) There must be a return value, the type of the return value is the same as the type of the member variable
- (2) The method name is the same as the member variable name
- (3) No need to receive any parameters
- The Get method uses an example:
- Declaration of the Get method:
- Get method implementation
Note 1: In real development, the set and get methods are not necessarily provided, if the internal member variables such as the student's school number such as data only allow the outside to read, but do not allow the modification of the situation, usually only provide a GET method and do not provide a set method. Note 2: The name of the member variable is preceded by an underscore, the Get method name does not need to be underlined, and two benefits are preceded by an underscore: (1) is distinguished from the method name of the Get method, (2) It can be distinguished from some other local variables, and variables that begin with an underscore are usually member variables of the class.Inheritance
- Basic concepts
The benefits of Inheritance: (1) extracting Duplicate code (2) establishes the disadvantage of the connection inheritance between classes and classes: coupling is too strong
2. Code:/********** book ************/@interface book:nsobject{int _name; int _author; Author}-(void) SetName: (int) name;-(int) name; @end @implementation book-(void) SetName: (int) name{_name = name;} -(int) name{return _name;} @end/********** man ************/@interface person:nsobject{int _age; Double _weight; Book *book; Combined relationship}-(void) study;+ (void) eat; @end//method implementation part omitted/********** student ************/@interface Student:person//student inherited the person-(void) Liudog; Walking the dog @end
3. Inheritance and CompositionApplication Scenarios for Inheritance: (1) When two classes have the same properties and methods, the same properties and methods can be extracted into a parent class. (2) When Class A fully possesses some of the properties and methods in Class B, it may be considered that Class B inherits Class A (consider), in which case a combination can also be considered. Inheritance: # # #是xxx, such as dogs are animals, can let dogs inherit the animal class combination: # # #拥有xxx, such as students have books, can let the book this class as a student class properties
4. Attention to the use of inheritance(1) The compiler executes from top to bottom, so there should be at least a declaration of the parent class in front of the subclass, (2) the child class and the parent class are not allowed to have the same name of the member variable name, (3) the subclass in OC can have a method with the same name as the parent class, and when the subclass is called, If there is no one layer to look up; Hint: the subclass re-implements a method in the parent class, overriding the previous implementation of the parent class. : There are three classes, the person class inherits from the NSObject class, and the student class inherits the person class. Create a Student *s=[[student alloc] init]; This will load the Student class and the class's parent class into memory. Tip: There is a super class pointer in each class that points to your parent class. There is an Isa pointer in the object that points to the class that called the object. polymorphicPolymorphism for the object-oriented thinking, the personal feeling is really important, he wrote code in the future elegant way also plays a very important role, in fact, many of the design patterns are used in most of the characteristics of polymorphic, in fact, many states are: the definition of the type and the actual type, is generally based on the form of an interface to achieve, Not much to say, look directly at the example:
Examples of printers
Abstract Printer Class PrinterPrinter.h
1.//
2.//Printer.h
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import <Foundation/Foundation.h>
10.
One by one .
@interfacePrinter:nsobject
12.
13.-(
void) Print;
14.
A .
@end
is a simple way to print
Printer.m
1.//
2.//PRINTER.M
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "Printer.h"
10.
One by one .
@implementationPrinter
12.
13.-(
void) print{
NSLog (@ "Printer printing paper");
15.}
16.
.
@end
The implementation is also very simple
Here's a look at the specific subclass ColorPrinter.h 1. //
2.//ColorPrinter.h
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "Printer.h"
10.
11.//Modify the print behavior of the parent class
.
@interfaceColorprinter:printer
13.-(
void) Print;
.
@end
Colorprinter.m
1.//
2.//COLORPRINTER.M
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "ColorPrinter.h"
10.
One by one .
@implementationColorprinter
12.
13.-(
void) print{
NSLog (@ "Color printer");
15.}
16.
.
@end
Take a look at another subclass
BlackPrinter.h 1. 2. BlackPrinter.h
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #
Import"Printer.h"
10.
One by one .
@interfaceBlackprinter:printer
12.
13.-(
void) Print;
14.
@end.
Blackprinter.m
1.//
2.//BLACKPRINTER.M
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "BlackPrinter.h"
10.
One by one .
@implementationBlackprinter
12.
13.-(
void) print{
NSLog (@ "Monochrome printer");
15.}
16.
.
@end
Here we are defining a person class that is used to manipulate the specific printer Person.h
1.//
2.//Person.h
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import <Foundation/Foundation.h>
10.
#import "ColorPrinter.h"
#import "BlackPrinter.h"
13.
14.//Scalability is not high, when we need to add a new printer to define a corresponding method
15.//So it's time to use polymorphic technology
16.
.
@interfaceperson:nsobject{
18.
NSString*_name;
19.}
20.
//-(void) Printwithcolor: (Colorprinter *) Colorprint;
22.
//-(void) Printwithblack: (Blackprinter *) blackprint;
24.
25.-(
void) Doprint: (
Printer*) printer;
26.
.
@end
PERSON.M 1. //
2.//PERSON.M
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "Person.h"
10.
One by one .
@implementationPerson
12.
13./*
-(void) Printwithcolor: (Colorprinter *) colorprint{
[Colorprint print];
16.}
17.
-(void) Printwithblack: (Blackprinter *) blackprint{
[Blackprint print];
20.}
21. */
22.
23.-(
void) Doprint: (
Printer*) printer{
[Printer print];
25.}
26.
.
@end
Let's take a look at the test code:
MAIN.M 1. //
2.//MAIN.M
3.//07_dynamicdemo
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import <Foundation/Foundation.h>
10.
#import "Person.h"
#import "BlackPrinter.h"
#import "ColorPrinter.h"
14.
A .
intMain
intargc
Const
Charchar* argv[]) {
16.
@autoreleasepool{
17.
18.
Person*person =[[person alloc] init];
19.
20.
Colorprinter*colorprint = [[Colorprinter alloc] init];
21st.
Blackprinter*blackprint = [[Blackprinter alloc] init];
22.
23.//Polymorphism Definition
24./*
Printer *P1 = [[Colorprinter alloc] init];
Printer *P2 = [[Blackprinter alloc] init];
27.
[Person DOPRINT:P1];
[Person DOPRINT:P2];
30. */
31.
32.//control which printer is used by the command entered by the console
33.
intcmd
34.
Do{
scanf ("%d", &cmd);
36.
if(cmd = = 1) {
[Person Doprint:colorprint];
38.}
Else
if(cmd = = 2) {
[Person Doprint:blackprint];
40.}
41.}
while(1);
42.
43.}
44.
return0;
45.}
Here is a detailed explanation of the benefits of polymorphism
The above example is a color printer and a black and white printer both printers, and then the person class has a method of printing, of course, this method requires a Printer object, if not implemented by the polymorphic mechanism (Person.h in the code part of the comment), is to define a separate operation for the two printers, and then use the specific Printer object in the PERSON.M (part of the code), and in the main.m file, we see that the specified method is called when the person needs to use the printer: 1. [Person printwithblack:blackprint];//call black and white printer
2. [Person printwithcolor:colorprint];//call Color Printer This design is not good, why? If there is another printer now, then we need to define a way to operate the printer in Person.h, so what if we add a new printer later? Are you still adding a method? Then the Person.h file becomes bloated. So this time polymorphism is the benefit, using the parent class type, define a method in Person.h: 1. - (
void) Doprint: (
Printer*) printer; Here we see that the parameter type of this method is the type of the parent class, which is polymorphic, the definition type is the parent class type, the actual type is the subclass type
1.-(
void) Doprint: (Printer *) printer{
2. [Printer print];
3. The Print method is called here, which is the actual type of print method passed in.
1.
Printer*P1 = [[Colorprinter alloc] init];
2.
Printer*P2 = [[Blackprinter alloc] init];
3.
4. [Person DOPRINT:P1];
5. [Person DOPRINT:P2];
The type on the P1,P2 surface here is printer, but the actual type is the subclass type, so it calls their own corresponding print method.
From the above example, we can see the polymorphism of the characteristics is very important, of course, the three characteristics of the more difficult to understand, but in the process of coding, with more natural understanding, there is no need to deliberately understand.
Summary
This article mainly introduces the three main characteristics of the class: encapsulation, inheritance, polymorphism, these three features are also the important basis behind learning object-oriented.
5 Object-oriented three major features