Mooc.com_ Learning summary of Objective-C object oriented initial experience

Time:2020-12-24

Time: Friday, April 28, 2017
Note: part of this article is from moocnet. @Moocnet:http://www.imooc.com
Teaching example source code: None
Personal learning source code:https://github.com/zccodere/s…

Chapter one: start the journey of object oriented

Introduction to 1-1 OOP

What is object oriented
OOP object oriented programming

OOA object oriented analysis
Ood object oriented design

Basic concepts of OOP

object
Abstract - class

Sketch Map

Mooc.com_ Learning summary of Objective-C object oriented initial experience

Mooc.com_ Learning summary of Objective-C object oriented initial experience

Abstract: from object to class; objectification: from class to object.

Mooc.com_ Learning summary of Objective-C object oriented initial experience

1-2 C + + and OC comparison

Mooc.com_ Learning summary of Objective-C object oriented initial experience

Mooc.com_ Learning summary of Objective-C object oriented initial experience

Chapter two: the relationship between class and object

2-1 OC creating classes and objects

Basic concepts of class and object

Create classes and get objects. The process from class to object is called instantiation

Code demonstration:

Mooc.com_ Learning summary of Objective-C object oriented initial experience

Mooc.com_ Learning summary of Objective-C object oriented initial experience

On oc-2 and oc-2

Declaration and use of member variables

Declaration and use of properties

Sketch Map

Mooc.com_ Learning summary of Objective-C object oriented initial experience

Code demonstration:

Mooc.com_ Learning summary of Objective-C object oriented initial experience

2-3 OC attributes and member variables (2)

Member variables are used within a class and are declared in the. M file

Attribute variables are used outside the class and are declared in the. H file

Complete code demonstration:

1. People class declaration

//
//  People.h
//  Class1
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

//Use member variables inside class and use attributes outside class
//Name, age, gender
#import <Foundation/Foundation.h>

@interface People : NSObject
/*
{    
    NSString *_peopleName;
    int _peopleAge;
    int _peopleSex;
}
*/
//To make member variables accessible outside the class
//Attributes are external interfaces to member variables
@property(nonatomic,strong)NSString *peopleName;

//- (void)setName:(NSString *)name;
//- (NSString *)getName;
@end

2. Implementation of people class

//
//  People.m
//  Class1
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "People.h"

@implementation People
{
    int _peopleAge;
    int _peopleSex;
}

- (instancetype) init{
    self = [super init];
    if(self){
        //Member variables are called inside the class instead of attributes, which are used outside the class
        A kind of Peoplename = @ "Zhang San";
    }
    return self;
}

/*
- (void)setName:(NSString *)name{
    _peopleName = name;
}
- (NSString *)getName{
    return _peopleName;
}
*/
@end

3. Main class

//
//  main.m
//  Class1
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>
//Declaration of import class
#import "People.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //Instantiation object, * represents pointer, that is, in OC, all objects are pointers.
        /*
         [class name method name]
         [object name method name]
         Alloc allocates memory space for objects
         Init to initialize
         */
        People *p1 = [[People alloc] init];
        People *p2 = [[People alloc] init];

        People * P3 = [people new]; // not recommended
        
        NSLog(@"p1 : %p",p1);
        NSLog(@"p2 : %p",p2);
        NSLog(@"p3 : %p",p3);
        
        P1. Peoplename = @ "Zhang San";
        P2. Peoplename = @ "Li Si";
        NSLog(@"people name : %@",p1.peopleName);
        NSLog(@"people name : %@",p2.peopleName);
    }
    return 0;
}

Chapter 3: functions of classes

3-1 OC function (1)

Sketch Map:

Mooc.com_ Learning summary of Objective-C object oriented initial experience

3-2 OC function (2)

Declaration method

1. Type of - + method
    -Represents an object method, called with the object name
    +Represents a class method, which is used to call
    The plus sign method and the minus sign method can be called each other. Of course, the class name and the instantiated object are required.
    The plus sign method cannot call member variables, it can call static member variables.
 2. (void) return value type
 
 3. Parameters
    : (int) x represents the parameter, (int) represents the parameter type, and X represents the parameter name
 4. Method name
    Get rid of method type, parameter type and parameter name, and the rest is method name

3-3 OC function (2)

OC initialization method, construction method of type Java

Complete code demonstration:

1. People class declaration

//
//  People.h
//  MethodDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>

@interface People : NSObject

/*
 Declaration method
 1. Type of - + method
    -Represents an object method, called with the object name
    +Represents a class method, which is used to call
    The plus sign method and the minus sign method can be called each other. Of course, the class name and the instantiated object are required.
    The plus sign method cannot call member variables, it can call static member variables.
 2. (void) return value type
 
 3. Parameters
    : (int) x represents the parameter, (int) represents the parameter type, and X represents the parameter name
 4. Method name
    Get rid of method type, parameter type and parameter name, and the rest is method name
 */

- (void)report;
/*
+ (void)report1;

- (int)showWithA:(int)a;

// showWithA: andB:
- (int)showWithA:(int)a andB:(int)b;
*/


//Initialization method
-(instancetype) init; // override init method
//Custom initialization method
- (instancetype)initWithPeopleName:(NSString *)
peopleName andPeopleAge:(int)peopleAge;


@end

2. Implementation of people class

//
//  People.m
//  MethodDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "People.h"

@implementation People
{
    //Member variable
    NSString *_peopleName;
    int _peopleAge;
}

//Static member variable
static NSString *_peopleName1;

- (void)report{
//Nslog (@ "- No.: report");
//    //[People report1];
//    _peopleName = @"123";
    NSLog(@"peopleName = %@",_peopleName);
    NSLog(@"peopleName = %d",_peopleAge);


}

+ (void)report1{
    Nslog (@ "+ No.: report1");
    [[People alloc] report];
    A kind of Three = "people1 @";
}

- (int)showWithA:(int)a{
    return a;
}

- (int)showWithA:(int)a andB:(int)b{
    return a+b;
}

//Override initialization method
- (instancetype)init
{
    self = [super init];
    if (self) {
        _peopleName = @"Visitor";
        _peopleAge = 30;
    }
    return self;
}

- (instancetype)initWithPeopleName:(NSString *)
peopleName andPeopleAge:(int)peopleAge{
    self = [super init];
    if(self){
        _peopleName = peopleName;
        _peopleAge = peopleAge;
    }
    return self;
}


@end

3. Main class

//
//  main.m
//  MethodDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "People.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //[] represents the calling method, and the calling method uses []
        People *p1 = [[People alloc] init];
        
//        [p1 report];
        
//        [People report1];
//        
//        int a1 = [p1 showWithA:10];
//        NSLog(@"a1 = %d",a1);
//        
//        int a2 = [p1 showWithA:10 andB:20];
//        NSLog(@"a2 = %d",a2);
        
        [p1 report];
        
        People * P2 = [[people alloc] initwithpeoplename: @ "Zhang San" and PeopleAge:20 ];
        People * P3 = [[people alloc] initwithpeoplename: @ "Li Si" and PeopleAge:30 ];

        [p2 report];
        [p3 report];
        
    }
    return 0;
}

Chapter four: object oriented trilogy

4-1 OC object oriented encapsulation (1)

What is encapsulation

It hides the properties and implementation details of the object, only exposes the interface to the public, and controls the access level of the read and modify properties in the program.

Benefits of encapsulation

1. Things will be more abstract, more convenient to use
2. Access control to achieve better results

4-2 OC object oriented encapsulation (Part 2)

Access modifier

@Public class can be used outside the class and can be inherited, which refers to method call (- >)
@It is used within the protected class and can be inherited
@It is used in private class and cannot be inherited
@Package framework permissions: protected within the framework and private outside the framework

Complete code demonstration:

1. MyClass class declaration

//
//  MyClass.h
//  PackDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MyClass : NSObject
{
    //The problem of member variable access modifier
    
    //The default is protected
    
    //Public: can be used inside and outside the class and can be inherited
    @public
    int _classInt;
    
    //Private: can be used within the class, cannot be called outside the class and cannot be inherited
    @private
    
    //Protected: default: it can be used inside the class, not called outside the class, and can be inherited
    @protected
    NSString *_classStr;
    
    //Framework permissions: protected within the framework, private outside the framework
    @package
    
}
@property(nonatomic,strong)NSString *className;

//Methods have no modifiers, just like C language
- (void)report;

@end

2. MyClass class implementation

//
//  MyClass.m
//  PackDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "MyClass.h"

@implementation MyClass


- (void)report{
    _classStr = @"234";
    NSLog(@"ClassName : %@",_className);
    NSLog(@"ClassName : %d",_classInt);
}

@end

3. Main class

//
//  main.m
//  PackDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MyClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        MyClass *mc = [[MyClass alloc] init];
        
        mc.className  [email protected] "my class";
        
        [mc report];
        //Use a pointer to call a public member variable in a class
        mc->_classInt = 1001;
        
        [mc report];
        
        
    }
    return 0;
}

4-3 OC object oriented inheritance (Part one)

What is inheritance? Subclasses inherit from their parents.
What is multi inheritance? There are many parent class inheritance. There is no multi inheritance in OC.

4-4 OC object oriented inheritance (2)

If the method in the parent class does not write a declaration, the child class cannot inherit the corresponding method in the parent class.

Complete code demonstration:

1. MyClass class declaration

//
//  MyClass.h
//  ExtendDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>

//Nsobject: base class
@interface MyClass : NSObject
{
    //Access modifiers are protected by default
    @protected
    int _classInt;
    
    //Private
    @private
    NSString *_classString;
}
@property(nonatomic,strong)NSString *className;

-(void)report;

@end

2. MyClass class implementation

//
//  MyClass.m
//  ExtendDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "MyClass.h"

@implementation MyClass

- (void)report{
    _classInt = 1001;
    _classString = @"MyClassStr";
    NSLog(@"ClassName : %@",_className);
    NSLog(@"ClassInt : %d",_classInt);
    NSLog(@"ClassString : %@",_classString);
}

@end

3. Mysubclass class declaration

//
//  MySubClass.h
//  ExtendDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

//Reference parent class header file
#import "MyClass.h"

//Interface, the colon (:) represents the inheritance relationship
@interface MySubClass : MyClass

- (void)show;

@end

4. Implementation of mysubclass

//
//  MySubClass.m
//  ExtendDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "MySubClass.h"

@implementation MySubClass

- (void)show{
    _classInt = 1002;
    //We cannot inherit and use private variables in the parent class
    //_classString = @"fdsf";
    NSLog(@"show : %d",_classInt);
    
    //If the method in the parent class does not write a declaration, the subclass cannot inherit the corresponding method in the parent class
    [self report];
}

@end

5. Main class

//
//  main.m
//  ExtendDemo
//
//  Created by zc on 2017/4/28.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MyClass.h"
#import "MySubClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //Parent class
        MyClass *mc = [[MyClass alloc] init];
        mc.className = @"MyClass";
        [mc report];
        
        //Subclass
        MySubClass *msc = [[MySubClass alloc] init];
        msc.className = @"MySubClass";
        [msc report];
        [msc show];
    }
    return 0;
}

4-5 OC object oriented polymorphism

Basic concepts of polymorphism

The same parent class with multiple subclasses and different morphology is called polymorphism.
Method rewriting and method overloading are necessary means of polymorphism.
Method overloading is not supported in OC.

Complete code demonstration:

1. Printer class declaration

//
//  Printer.h
//  DuoTaiDemo
//
//  Created by zc on 2017/5/10.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface Printer : NSObject

- (void)print;

@end

2. Printer class implementation

//
//  Printer.m
//  DuoTaiDemo
//
//  Created by zc on 2017/5/10.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "Printer.h"

@implementation Printer

- (void)print{
    Nslog (@ "I am a printer");
}

@end

3. Colorprinter class declaration

//
//  ColorPrinter.h
//  DuoTaiDemo
//
//  Created by zc on 2017/5/10.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "Printer.h"

@interface ColorPrinter : Printer

@end

4. Implementation of colorprinter class

//
//  ColorPrinter.m
//  DuoTaiDemo
//
//  Created by zc on 2017/5/10.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "ColorPrinter.h"

@implementation ColorPrinter

//Method override
- (void)print{
    Nslog (@ "color printer");
}

@end

5. Blackprinter class declaration

//
//  BlackPrinter.h
//  DuoTaiDemo
//
//  Created by zc on 2017/5/10.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "Printer.h"

@interface BlackPrinter : Printer

@end

6. Printer color class implementation

//
//  BlackPrinter.m
//  DuoTaiDemo
//
//  Created by zc on 2017/5/10.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import "BlackPrinter.h"

@implementation BlackPrinter

//Method override
- (void)print{
    
    //Call the parent class method
    [super print];
    
    //Call the current class method
    //[self print];
    
    Nslog (@ "black and white printer");
}

@end

7. Main class

//
//  main.m
//  DuoTaiDemo
//
//  Created by zc on 2017/5/10.
//Copyright? 2017 com.zccoder . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ColorPrinter.h"
#import "BlackPrinter.h"


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        ColorPrinter *cp = [[ColorPrinter alloc] init];
        
        BlackPrinter *bp = [[BlackPrinter alloc] init];
        
        [cp print];
        
        [bp print];
        
        Printer *printer = [[ColorPrinter alloc] init];
        [printer print];
        
    }
    return 0;
}

Recommended Today

DK7 switch’s support for string

Before JDK7, switch can only support byte, short, char, int or their corresponding encapsulation classes and enum types. After JDK7, switch supports string type. In the switch statement, the value of the expression cannot be null, otherwise NullPointerException will be thrown at runtime. Null cannot be used in the case clause, otherwise compilation errors will […]