Super keyword 1, OC inheritance syntaxThe OC syntax only supports single inheritance, that is, a class can only have one parent class.
The inherited keyword is:
@ Interface category: parent class name
For example, the triangle class we declared yesterday
@interface Triangle : NSObject@end
Indicates that the Triangle class is inherited from the NSObject class. Triangle is a subclass of NSObject, and NSObject is the parent class of Triangle.
The OC syntax specifies that all methods of the parent class and all member variables except private permissions can be inherited through inheritance.
For example, the triangle type in the previous chapter
@interface Triangle : NSObject@property double a;@property double b;@property double c;-(instancetype)initWithA:(double)a B:(double)b C:(double)c;-(double)area;@end
@implementation Triangle- (instancetype)initWithA:(double)a B:(double)b C:(double)c{ self = [super init]; if (self) { self.a = a; self.b = b; self.c = c; } return self;}-(double)area{ double s=(_a+_b+_c)/2; return sqrt(s*(s-_a)*(s-_b)*(s-_c));}
In this chapter, we can create a new triangle class using an inherited method.
@interface TriangleSub : Triangle@end
@implementation TriangleSub@end
We can see that the newly created TriangleSub class does not declare any methods or implement any methods. However, through the inheritance syntax, we obtain all the methods and member variables of the parent class for free.
TriangleSub * subT = [[TriangleSub alloc] initWithA: 10 B: 11 C: 12]; double s = [subT area]; NSLog (@ "s = % g ", s); // The NSLog function is a function that outputs information to the console in OC.
2. Dynamic Features of OC LanguageThe OC language is a dynamic language. When calling a method, the system does not call the object method based on the object pointer type, but calls the response method based on the memory type used by the object pointer.
The following code describes this feature.
First, create a test class TestClass.
@interface TestClass : NSObject-(void)test;@end
@ Implementation TestClass-(void) test {NSLog (@ "here is the test method of the TestClass class");} @ end
We continue to create two classes:
SubA inheritance and TestClass
@interface SubA : TestClass@end
@ Implementation SubA // override the method of the parent class-(void) test {NSLog (@ "here is the test method of the SubA class");} @ end
SubB inheritance and TestClass
@interface SubB : TestClass@end
@ Implementation SubB // override the method of the parent class-(void) test {NSLog (@ "here is the test method of the SubB class");} @ end
Now do the following test
SubA * a = [[SubB alloc] init]; [a test]; // this way, no compilation error is called. If the Composite OC syntax rule is used, which result will be output.
Based on what we have discussed earlier, the presentation of the dynamic characteristics of OC
The Object Pointer does not call the response method based on the object pointer type, but calls the response method based on the memory type pointed to by the object pointer.
It can be seen that the object pointera
IsSubA
Type, But it points to the memory typeSubB
Type. Therefore, when the test method is called with an object pointerSubB
.
The output result is: the test method of the SubB class.
Because of the dynamic characteristics of OC syntax, the object pointer type only works during compilation during code writing. There is no impact in the running process.
OC syntax introducesid
Keyword, indicating that the type is not specified during compilation.
Useid
Writing type code does not affect the operation. However, in the IDE environment, you cannot enable the code auto-completion function. Because the IDE tool does not know the type of the Object Pointer, you cannot automatically prompt and complete the code.
3. OC composite modeIn the composite mode of OC, other objects are taken as part of their own questions to improve their functions.
For example, if I want to create a computer, the computer needs CPU, display, mouse, and keyboard. R & D of these things is a complicated process. If you have a CPU or other components, you can directly use these components to collect a computer. This is the combination mode.
All the components we need to find before creating a computer. These components do not need to be made. Professional companies provide these components.
The following components are provided in the SDK of this chapter.
KeyBoard type BigMouse type Monitor display type IntelCPU type
Next we will use the various components provided in the SDK to quickly create a computer.
First, we declare the computer. All components to be used are declared as attributes and are part of the computer class.
@interface Computer : NSObject@property(strong, nonatomic) KeyBoard * aKeyBoard;@property(strong, nonatomic) BigMouse * aMouse;@property(strong, nonatomic) Monitor * aMonitore;@property(strong, nonatomic) IntelCPU * aCPU;@end
Here we will first add the content in the attribute brackets.
The content in the attribute brackets is the attribute control operator, which specifies the read and write permissions, memory management, and thread operations of the attributes.
- Read/write permissions are controlled by two keywords:
readonly
Andreadwrite
. If this parameter is not specified, the default value isreadwrite
. Generally, the read and write permissions of an attribute are readable and writable.
- The memory management permission is controlled by four keywords:
assgin
,strong
,weak
Andcopy
. If not written, the default value isassgin
. If the attribute type is basic data type, you can only useassgin
. If the property type is an object pointerstrong
, Used in special casesweak
Orcopy
. Special cases will be explained in the memory management section later.
- The thread operation permission is controlled by two keywords:
atomic
Andnonatomic
. If this parameter is not specified, the default value isatomic
. We usually usenonatomic
As a thread operation, the specific content will be explained in the subsequent multi-threaded section.Then we continue to implement the computer. Because CPU and other components are part of the computer, we need to re-initialize the computer, initialize various components at the same time, and allocate memory for them.
@implementation Computer- (instancetype)init{ self = [super init]; if (self) { self.aKeyBoard = [[KeyBoard alloc] init]; self.aMouse = [[BigMouse alloc] init]; self.aMonitore = [[Monitor alloc] init]; self.aCPU = [[IntelCPU alloc] init]; } return self;}@end
After declaring component properties, allocate memory to the object pointer of the component. We used the components to quickly create a fully functional computer.
4, super keywordInheritance and combination are usually used together to quickly construct a complex object.
For example, there is a demand that requires us to quickly create an advanced air conditioner that can remove formaldehyde. Generally, we do not start R & D from scratch, but create based on a certain foundation.
Here, I provide two objects as the basis for R & D.
- Kongtiao class with cooling method
- ChuJiaquan class, with methods for formaldehyde Removal
The following is the declaration of the Kongtiao class:
@interface Kongtiao : NSObject-(void)zhiling;@end
The following is the declaration of the ChuJiaquan class:
@interface ChuJiaQuan : NSObject-(void)chuJQ;@end
First, we made it clear that we made an air conditioner. All of us created an advanced air conditioner class and inherited it.
Then advanced air conditioners need the formaldehyde removal function. We use the compound mode to take the formaldehyde removal module as part of the advanced air conditioner.
Finally, we will rewrite the cooling method of the parent class and add the formaldehyde removal step during the cooling process. However, the method to be overwritten will overwrite the original method of the parent class. Here we need to manually call the original method of the parent class in the override method. You need to usesuper
Keyword.
@interface NewKongtiao : Kongtiao@property(strong, nonatomic) ChuJiaQuan * cJQ;@end
@implementation NewKongtiao- (instancetype)init{ self = [super init]; if (self) { self.cJQ = [[ChuJiaQuan alloc] init]; } return self;}-(void)zhileng{ [super zhileng]; [self.cJQ chuJQ];}@end
In this way, while using advanced air conditioners for refrigeration, we can also remove formaldehyde.