一:#import 跟#include 有什么區(qū)別
import指令是Object-C針對(duì)#include的改進(jìn)版本衷蜓,
import確保引用的文件只會(huì)被引用一次,這樣就不會(huì)陷入遞歸包含的問(wèn)題中尘喝。
二:構(gòu)造方法
// 構(gòu)造函數(shù)
1.一般語(yǔ)言中磁浇,在對(duì)象初始化時(shí)掉用的方法,叫構(gòu)造方法
2.OC :init 開(kāi)頭的成員方法(對(duì)象方法)就是構(gòu)造方法
【構(gòu)造方法一般用于對(duì)象的初始化】
- (void)initsdfsfjkalsdjflkadsfklasjfklsa;
#import <Foundation/Foundation.h>
#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);
// 一個(gè)對(duì)象只有一個(gè)初始化方法朽褪,調(diào)用一次之后不能再次調(diào)用
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 <Foundation/Foundation.h>
@interface Apple : NSObject
{
@public;
int _count;
}
// id 萬(wàn)能指針可以指向任何類(lèi)型
// 構(gòu)造函數(shù)
// 1. 無(wú)參構(gòu)造函數(shù)
- (instancetype)init;
// 2.有參的構(gòu)造函數(shù)
- (instancetype)initWithCount:(int)count;
@end
#import "Apple.h"
@implementation Apple
- (instancetype)init
{
// self 當(dāng)前對(duì)象
// super 當(dāng)前類(lèi)的父類(lèi)
// [super init] 初始化父類(lèi)
// 1. 加載父類(lèi)資源
if (self = [super init]) {
// 初始化對(duì)象
// 2.加載自己的資源
_count = 10;
}
// 3. 資源加載完成返回
return self;
}
- (instancetype)initWithCount:(int)count
{
if (self = [super init]) {
_count = count;
}
return self;
}
@end
三:對(duì)象方法置吓,類(lèi)方法
#import <Foundation/Foundation.h>
#import "Car.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Car *car = [[Car alloc] init];
// [car run];
[Car run];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
float _speed;
}
// 成員方法/對(duì)象方法
// 減號(hào)開(kāi)頭
// 對(duì)象調(diào)用
- (void)run;
// 類(lèi)方法 加號(hào)開(kāi)頭
// 類(lèi)調(diào)用
+ (void)run;
// 類(lèi)方法的好處
//1. 在不使用成員變量的時(shí)候,可以不用創(chuàng)建對(duì)象缔赠,執(zhí)行某些方法
// 2. 可以讓我們的接口更加簡(jiǎn)潔
@end
#import "Car.h"
@implementation Car
- (void)run
{
NSLog(@"你的拉博基尼沒(méi)電了");
NSLog(@"%f",_speed);
}
+ (void)run
{
NSLog(@"O(∩_∩)O哈哈~");
Car *car = [[Car alloc] init];
NSLog(@"%f",car -> _speed);
}
@end
構(gòu)造函數(shù)練習(xí)
#import <Foundation/Foundation.h>
//#import "Apple.h"
#import "Apple.h"
#import "Human.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 獲取蘋(píng)果對(duì)象
Apple *apple10 = [[Apple alloc] initWithCount:10];
Apple *apple20 = [[Apple alloc] initWithCount:20];
// 獲取人對(duì)象
Human *xiaoMing = [[Human alloc] initWithApple:apple10];
Human *xiaoHuang = [[Human alloc] initWithApple:apple20];
// 以后獲取對(duì)象必須調(diào)用構(gòu)造函數(shù)
Human *xxx = [[Human alloc] init];
// 計(jì)算結(jié)果
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 <Foundation/Foundation.h>
#import "Apple.h"
@interface Human : NSObject
{
@public;
Apple *_apple;
}
- (instancetype)initWithApple:(Apple *)apple;
@end