#include与#import的区别:两者的作用效果相同,只是后者不会引起交叉编译,确保头文件只会被导入一次
#import与@class区别:#import会包含这个类的所有信息,包括实体变量和方法,而@class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,暂时不用考虑,后面会再告诉你。使用#import编译效率高,防止相互包含的编译错误!
Main.m
/*
improt和include都是导入文件,
区别是:include只是单纯的复制,
但是import是在导入之前会判断当前文件是否存在已有的文件,
如果没有再导入,否则不会导入
*/
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
Student.h
//#import "Classes.h"
/*
@class 可以解决互相导入的问题
@class的作用:告诉编译器在其他地方已经定义了这个类,具体的属性和方法当前文件不知道
*/
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: Menlo; min-height: 18px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);">
</p>
@class Classes;
@interface Student : NSObject {
Classes *classes; //班级
}
- (void)study;
Student.m
- (void)study {
NSLog(@"学生在学习");
}
Classes.h
//#import "Student.h"
//注意:继承的时候不可以在头文件中使用@class,只能只有#import
//@class Student;
@class Student;
@interface Classes : NSObject{
Student *student;
}