一絮宁、概念
二梆暮、代碼
#import <Foundation/Foundation.h>
#pragma mark 類
#pragma mark - 2.槍
@interface Gun : NSObject
{
@public
int _bullet; // 子彈
}
- (void)shoot;
@end
@implementation Gun
- (void)shoot
{
if (_bullet > 0)
{
_bullet--;
NSLog(@"打了一槍 %i",_bullet);
}
else
{
NSLog(@"沒有子彈了,請(qǐng)換彈夾");
}
}
@end
#pragma mark - 1.士兵
@interface Soldier : NSObject
{
@public
NSString *_name;
double _height;
double _weight;
}
- (void)fire:(Gun *)gun;
@end
@implementation Soldier
- (void)fire:(Gun *)g
{
[g shoot];
}
@end
#pragma mark - 3.彈夾
#pragma mark - main函數(shù)
int main(int argc, const char * argv[])
{
Soldier *s = [Soldier new];
s->_name = @"lyh";
s->_height = 1.71;
s->_weight = 65.0;
Gun *gp = [Gun new];
gp->_bullet = 10;
[s fire:gp]; // 地址
[s fire:gp];
[s fire:gp];
[s fire:gp];
[s fire:gp];
[s fire:gp];
return 0;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者