如何添加外部定义的类

1: 如何添加外部定义的类

创建一个student. H文件以及student。M文件,然后我们就可以在其他的位置添加这个类的引用。

下面的参考实例代码如下:

类的接口的头文件的代码å

#import <Foundation/Foundation.h>

@interface student : NSObject {

    int age;

}

-(void) SetAge:(int)_age;

-(void) ShowAge;

@end

类实现文件的代码

#import "student.h"

#import <Foundation/Foundation.h>

@implementation student

-(void) SetAge:(int)_age

{

    age = _age;

}

-(void) ShowAge{

    printf("%i",age);

}

@end

添加这个类的文件的参考代码

#import <Foundation/Foundation.h>

#include "student.h"

int main (int argc, const char * argv[])

{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    student* stu = [student new];

    [stu SetAge:100];

    [stu ShowAge];

    [pool drain];

    return 0;

}

这里唯一需要注意的地方是:如果添加自己定义的类的文件,在引用他的时候的语法是这样的:

#import  “student.h”

或者是#include “student.h”

而不是:#import  <student.h>

这个也是我犯错的地方,所以我才将他记录下来。

原文地址:https://www.cnblogs.com/xingchen/p/2057744.html