Objective-c入门-第一个自定义类(6)

本节我们要实现一个自定义的BNRPerson类。

  • BNRPersn.h称为头文件或接口文件,包含实例变量和方法的声明
  • BNRPerson.m称为实现文件,包含所有方法的代码实现

目录

一、使用Xcode创建一个类

二、存取方法

三、存取方法的命名规范

四、self

 五、类前缀

六、练习


 

一、使用Xcode创建一个类

我们可以鼠标右键项目名称,选择New File…

或者通过另一种方式:

 以上方式任选一种,之后我们都会看到同一种弹框,如下所示:

点击Cocoa Class就可以创建类了。

输入类名就OK了。

我的长这样👀

 打开BNRPerson.h,声明两个实例变量

@interface BNRPerson : NSObject
{
    //BNRPerson类拥有两个实例变量
    float _heightInMeters;  //身高
    int _weightInKilos;     //体重
}
@end

头文件是以@interface开始,以@end结束。

声明实例变量要在花括号里面,否则float你都打不出来滴!

接下来,在花括号外面声明5个实例方法:

@interface BNRPerson : NSObject
{
    //BNRPerson类拥有两个实例变量
    float _heightInMeters;  //身高
    int _weightInKilos;     //体重
}
//BNRPerson类声明实例方法
-(float) heightInMeters;
-(void) setHeightInMeters:(float) h;
-(int) weightInKilos;
-(void) setWeightInKilos:(int) w;
//BNRPerson类拥有计算Body Mass Index的方法
-(float) bodyMassIndex;
@end

使用键盘快捷键 Ctrl + Command + ⬆️,可以返回BNRPerson.m文件。可以使用这个快捷键在头文件和类的实现文件之间切换。

接下来在.m文件中实现.h文件中声明的实例方法。

#import "BNRPerson.h"

@implementation BNRPerson
-(float) heightInMeters
{
    return _heightInMeters;
}
-(void) setHeightInMeters:(float) h
{
    _heightInMeters = h;
}
-(int) weightInKilos
{
    return _weightInKilos;
}
-(void) setWeightInKilos:(int) w
{
    _weightInKilos = w;
}
//BNRPerson类拥有计算Body Mass Index的方法
-(float) bodyMassIndex
{
    return _weightInKilos / (_heightInMeters * _heightInMeters);
}
@end

打开main.m,导入BNRPerson.h文件,

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

尖括号和双引号的区别?

尖括号告诉编译器可以在苹果库中找到,是预编译的头文件

双引号则告诉编译器在当前的项目中寻找头文件。

然后我们在main()添加使用BNRPerson类的方法。

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建BNRPerson实例
        BNRPerson* mikey = [[BNRPerson alloc] init];
        
        //使用setter方法为实例变量赋值
        [mikey setHeightInMeters:1.62];
        [mikey setWeightInKilos:46];
        
        //打印实例变量的值
        float height = [mikey heightInMeters];
        int weight = [mikey weightInKilos];
        NSLog(@"mikey is %.2f meters tall and weight is %d kilograms", height, weight);
        
        //使用实例变量计算bmi的值
        float bmi = [mikey bodyMassIndex];
        NSLog(@"mikey has a BMI of %f", bmi);
    }
    return 0;
}

二、存取方法

heightInMeters和weightInKilos是取方法。通过取方法,其他方法可以读取相应的实例变量。

setHeightInMeters:和setWeightInKilos:方法是存方法。通过存方法其他方法可以为相应的实例变量赋值。

存方法和取方法统称为存取方法。

三、存取方法的命名规范

取方法的名字和相应的实例变量一样,只是去掉实例变量开头的下划线。

//实例变量
float _heightInMeters;  //身高

//所对应的取方法
-(float) heightInMeters;

存方法以set开头,后面跟上去掉下划线的实例变量名。set后面的第一个字母要大写。

//实例变量
float _heightInMeters;  //身高

//所对应的存方法,既然是存,一定要参数呀!
-(void) setHeightInMeters:(float) h;

四、self

Objective-c的方法都包含一个隐含的局部变量self。self是指针,指向运行当前方法的对象。当某个对象要向自己发送消息时,就需要使用self。

例如在.m文件中计算BMI的方法可以改写成如下的形式。

//BNRPerson类拥有计算Body Mass Index的方法
-(float) bodyMassIndex
{
    float h = [self heightInMeters];
    return [self weightInKilos] / (h * h);
}

此外还可以将self作为实惨传给其他的方法,以便其访问“当前的”对象。

 五、类前缀

Objective-c没有命名空间。也就是说,如果你写了一个称为Person的类,而它连接到另一个库,其中有其他人声明的Person类,编译器无法区别这两个类,就会出现编译器错误。

为了避免这样的名字冲突。苹果公司推荐使用三个或三个以上字母作为类的前缀,让类的名字独一无二。

由于书中用的是BNR,我也就是BNR了。😎

六、练习

 BNRStockHolding.h文件

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface BNRStockHolding : NSObject
{
    //实例变量
    float _purchaseSharePrice;
    float _currentSharePrice;
    int _numberOfShares;
}
//针对各个实例变量创建相应的存取方法
-(float) purchaseSharePrice;
-(void) setPurchaseSharePrice:(float) purchase;
-(float) currentSharePrice;
-(void) setCurrentSharePrice:(float) current;
-(int) numberOfShares;
-(void) setNumberOfShares:(int) number;
//两个额外的实例方法
-(float) costInDollars;
-(float) valueInDollars;
@end

NS_ASSUME_NONNULL_END

BNRStockHolding.m文件 

#import "BNRStockHolding.h"

@implementation BNRStockHolding
//针对各个实例变量创建相应的存取方法
-(float) purchaseSharePrice
{
    return _purchaseSharePrice;
}
-(void) setPurchaseSharePrice:(float) purchase
{
    _purchaseSharePrice = purchase;
}
-(float) currentSharePrice
{
    return _currentSharePrice;
}
-(void) setCurrentSharePrice:(float) current
{
    _currentSharePrice = current;
}
-(int) numberOfShares
{
    return _numberOfShares;
}
-(void) setNumberOfShares:(int) number
{
    _numberOfShares = number;
}
//两个额外的实例方法
-(float) costInDollars
{
    return [self purchaseSharePrice] * [self numberOfShares];
}
-(float) valueInDollars
{
    return [self currentSharePrice] * [self numberOfShares];
}
@end

main.m文件

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建NSMutableArray对象和三个BNRStockHolding实例
        NSMutableArray* myArray = [NSMutableArray array];
        BNRStockHolding* stockOne = [[BNRStockHolding alloc] init];
        BNRStockHolding* stockTwo = [[BNRStockHolding alloc] init];
        BNRStockHolding* stockThree = [[BNRStockHolding alloc] init];
        [stockOne setPurchaseSharePrice:2.30];
        [stockOne setCurrentSharePrice:4.50];
        [stockOne setNumberOfShares:40];
        [stockTwo setPurchaseSharePrice:12.19];
        [stockTwo setCurrentSharePrice:10.56];
        [stockTwo setNumberOfShares:90];
        [stockThree setPurchaseSharePrice:45.10];
        [stockThree setCurrentSharePrice:49.51];
        [stockThree setNumberOfShares:210];
        [myArray addObject:stockOne];
        [myArray addObject:stockTwo];
        [myArray addObject:stockThree];
        int i = 0;
        for(BNRStockHolding* data in myArray){
            //英文不好,改为中文了,莫怪
            NSLog(@"第 %d 个数据的信息如下:",++i);
            NSLog(@"purchaseSharePrice:%.2f,currentSharePrice:%.2f,numberOfShares:%d", [data purchaseSharePrice], [data currentSharePrice],[data numberOfShares]);
        }
    }
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值