注意:通過new創建出來的的局部對象變量存儲在堆內存中,堆中的數據不會自動釋放。而存儲在棧內存中的局部變量會自動釋放掉(比如C語言中的基本數據類型的變量)
#pragma mark - 商店
@interface Shop : NSObject
// 買槍
+ (Gun *)buyGun:(int)money;
// 買彈夾
+ (Clip *)buyClip:(int)money;
@end
@implementation Shop
+ (Gun *)buyGun:(int)money
{
// 1.創建一把槍
Gun *gun = [Gun new]; // 通過new創建出來的對象存儲在堆中, 堆中的數據不會自動釋放
// 2.返回一把槍
return gun;
}
+ (Clip *)buyClip:(int)money
{
Clip *clip = [Clip new];
[clip addBullet];
return clip;
}
@end