iOS入门学习(Objective-c类的声明与实现)

OC类的声明与实现

在ios开发中,使用的各种方法都是来自于不同的类,学习oc语言,类的使用更为基础。
在写代码之前,我们先得了.h文件和.m文件分别代表什么含义。
.h是声明文件,声明我们即将创建的类的属性,方法
.m是实现文件,实现我们在声明文件中所定义的方法  .m文件可以用c或者oc编写

我们新建一个Student类
Student.h
#import <Foundation/Foundation.h>

@interface Student : NSObject{
    NSString *name;
    int age;
}

-(void)say;

@end

Student.m
#import "Student.h"

@implementation Student

-(void)say{
    NSLog(@"hell");
}

@end

使用我们创建的Studentl类
#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *s = [[Student alloc] init];
        [s say];
        
    }
    return 0;
}


我们可以看到,初始化Student的代码
 Student *str = [[Student alloc] init];
我们可以把这部分代码分为三个部门:
Student *str  使用Student ,创建一个指针str 
[Student alloc]  使用alloc为Student申请使用内存
init  初始化Student
这样,我们实例化一个Student

你可能感兴趣的:(iOS入门学习(Objective-c类的声明与实现))