OC tutorial 2 Inheritance and combination

Source: Internet
Author: User

OC tutorial 2 Inheritance and combination
OC2

This chapter mainly discusses the inheritance Syntax of OC and the composite programming mode of classes.

  1. OC inheritance syntax
  2. Dynamic Features of OC Language
  3. OC composite mode
  4. Super keyword 1, OC inheritance syntax

    The 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 Language

    The 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 pointeraIsSubAType, But it points to the memory typeSubBType. 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 introducesidKeyword, indicating that the type is not specified during compilation.

    UseidWriting 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 mode

    In 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.

    1. Read/write permissions are controlled by two keywords:readonlyAndreadwrite. If this parameter is not specified, the default value isreadwrite. Generally, the read and write permissions of an attribute are readable and writable.
    2. The memory management permission is controlled by four keywords:assgin,strong,weakAndcopy. 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 casesweakOrcopy. Special cases will be explained in the memory management section later.
    3. The thread operation permission is controlled by two keywords:atomicAndnonatomic. If this parameter is not specified, the default value isatomic. We usually usenonatomicAs 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 keyword

      Inheritance 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.

      1. Kongtiao class with cooling method
      2. 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 usesuperKeyword.

        @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.

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.