IOS-OC-头文件引用、构造方法、对象和类方法

一:#import 跟#include 有什么区别

import指令是Object-C针对#include的改进版本,

import确保引用的文件只会被引用一次,这样就不会陷入递归包含的问题中。

二:构造方法

// 构造函数
1.一般语言中,在对象初始化时掉用的方法,叫构造方法

2.OC :init 开头的成员方法(对象方法)就是构造方法

【构造方法一般用于对象的初始化】

  • (void)initsdfsfjkalsdjflkadsfklasjfklsa;
#import 
#import "Apple.h"

int main(int argc, const char * argv[]) {
   @autoreleasepool {
//        Apple *app = [[Apple alloc] init];
       Apple *apple = [Apple alloc];
       Apple *appleInit = [apple init];
       NSLog(@"%d",apple->_count);
       
       // 一个对象只有一个初始化方法,调用一次之后不能再次调用
        Apple *apple1 = [[Apple alloc] initWithCount:20];
        Apple *a = [apple1 initWithCount:200];
        Apple *b = [apple1 init];
        NSLog(@"%d",b->_count);
       
       Apple *apple2 = [[Apple alloc] initWithCount:100];
       NSLog(@"%d",apple2->_count);
       
       
       
       
   }
   return 0;
}

#import 

@interface Apple : NSObject
{
@public;
    int _count;
}
// id 万能指针可以指向任何类型
// 构造函数
// 1. 无参构造函数
- (instancetype)init;

// 2.有参的构造函数
- (instancetype)initWithCount:(int)count;
@end

#import "Apple.h"

@implementation Apple
- (instancetype)init
{
    // self 当前对象
    // super 当前类的父类
    // [super init] 初始化父类
    
    // 1. 加载父类资源
    if (self = [super init]) {
        // 初始化对象
        // 2.加载自己的资源
        _count = 10;
        
    }
    
    // 3. 资源加载完成返回
    return self;
}

- (instancetype)initWithCount:(int)count
{
    if (self = [super init]) {
        _count = count;
    }
    return self;
}
@end

三:对象方法,类方法


#import 
#import "Car.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        Car *car = [[Car alloc] init];
//        [car run];
        
        [Car run];
    }
    return 0;
}
#import 

@interface Car : NSObject
{
    float _speed;
}
// 成员方法/对象方法
// 减号开头
// 对象调用
- (void)run;

// 类方法 加号开头
// 类调用
+ (void)run;

// 类方法的好处
//1. 在不使用成员变量的时候,可以不用创建对象,执行某些方法

// 2. 可以让我们的接口更加简洁

@end
#import "Car.h"

@implementation Car
- (void)run
{
    NSLog(@"你的拉博基尼没电了");
    NSLog(@"%f",_speed);
}

+ (void)run
{
    NSLog(@"O(∩_∩)O哈哈~");
    Car *car = [[Car alloc] init];
    NSLog(@"%f",car -> _speed);
}
@end

构造函数练习

#import 
//#import "Apple.h"
#import "Apple.h"
#import "Human.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 获取苹果对象
        Apple *apple10 = [[Apple alloc] initWithCount:10];
        Apple *apple20 = [[Apple alloc] initWithCount:20];
        
        // 获取人对象
        Human *xiaoMing = [[Human alloc] initWithApple:apple10];
        Human *xiaoHuang = [[Human alloc] initWithApple:apple20];
        
        // 以后获取对象必须调用构造函数
        Human *xxx = [[Human alloc] init];
        
        // 计算结果
        int count = xiaoMing->_apple->_count + xiaoHuang->_apple->_count;
    }
    return 0;
}
#import "Apple.h"

@implementation Apple
- (instancetype)initWithCount:(int)count
{
    if (self = [super init]) {
        _count = count;
    }
    return self;
}
@end
#import 
#import "Apple.h"

@interface Human : NSObject
{
    @public;
    Apple *_apple;
}

- (instancetype)initWithApple:(Apple *)apple;
@end

你可能感兴趣的:(IOS-OC-头文件引用、构造方法、对象和类方法)