obejctive-c 类的声明与使用(外部文件导入的方式)

obejctive-c 类的声明与使用(外部文件导入的方式),第1张

1.新建类的方式


2.Teacher.m的代码

//
//  Teacher.m
//  hello
//
//  Created by 老米 on 2021/11/10.
//

#import "Teacher.h"

@implementation Teacher
-(void)say
{
    NSLog(@"Hello, Teacher!");
}
@end

3.Teacher.h的代码

//
//  Teacher.h
//  hello
//
//  Created by 老米 on 2021/11/10.
//

#import <Foundation/Foundation.h>

@interface Teacher : NSObject
{
    //类的属性声明
    NSString *name;
    int age;
}
//类方法声明
-(void)say;
@end

4.引入类以及调用类方法

//
//  main.m
//  hello
//
//  Created by 老米 on 2021/11/10.
//

#import <Foundation/Foundation.h>
#import "Teacher.h"

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/996771.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存