栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【Objective-C语言 面向对象版的士兵突击】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Objective-C语言 面向对象版的士兵突击】

前言

士兵突击,是用Objective-C语言实现的一个面向对象的案例。

一、原理分析

士兵要射击,需要有枪。枪要开火,需要有弹夹。弹夹装子弹,需要判断子弹数量是否大于最大装弹量。
原则就是自己的事情自己做,判断装弹量是否大于0,并且小于最大装弹量,是弹夹的事儿。
判断弹夹里是否还有子弹,是枪在射击时的事儿,如果有子弹,则可以射击。
士兵射击,需要有把枪,士兵有了枪才能射击。

二、代码如下: 1.DanJia.h

#import

/// 弹夹类
@interface DanJia : NSObject
{
///最大装弹数
int _maxCapacity;
///现有子弹数
int _bulletCount;
}

  • (void)setMaxCapacity:(int)maxCapacity;

  • (int)maxCapacity;

  • (void)setBulletCount:(int)bulletCount;

  • (int)bulletCount;
    @end

2.DanJia.m

#import “DanJia.h”

@implementation DanJia

  • (void)setMaxCapacity:(int)maxCapacity
    {
    _maxCapacity = maxCapacity;
    }

  • (int)maxCapacity
    {
    return _maxCapacity;
    }

  • (void)setBulletCount:(int)bulletCount
    {
    if(bulletCount>= 0 && bulletCount <= _maxCapacity)
    {
    _bulletCount = bulletCount;
    }
    else
    {
    _bulletCount = 10;
    }

}

  • (int)bulletCount
    {
    return _bulletCount;
    }
    @end
3.Gun.h


#import
#import “DanJia.h”
///枪类
@interface Gun : NSObject
{
NSString *_model;
int _sheCheng;
DanJia *_danJia;
//int _bulletCount;
}

  • (void)setModel:(NSString *)model;

  • (NSString *)model;

  • (void)setSheCheng:(int)sheCheng;

  • (int)sheCheng;

  • (void)setDanJia:(DanJia *)danJia;

  • (DanJia *)danJia;

//- (void)setBulletCount:(int)bulletCount;
//- (int)bulletCount;

  • (void)shoot;
    @end
4.Gun.m

#import “Gun.h”

@implementation Gun

  • (void)setModel:(NSString *)model
    {
    _model = model;
    }

  • (NSString *)model
    {
    return _model;
    }

  • (void)setSheCheng:(int)sheCheng
    {
    _sheCheng = sheCheng;
    }

  • (int)sheCheng
    {
    return _sheCheng;
    }

  • (void)setDanJia:(DanJia *)danJia
    {
    _danJia = danJia;
    }

  • (DanJia *)danJia
    {
    return _danJia;
    }

//- (void)setBulletCount:(int)bulletCount
//{
// _bulletCount = bulletCount;
//}
//- (int)bulletCount
//{
// return _bulletCount;
//}

  • (void)shoot
    {
    //1.先判断当前枪对象的弹夹中是否还有子弹
    if([_danJia bulletCount] > 0)
    {
    NSLog(@“突突突…”);
    //突突突完以后,弹夹里的子弹数量要减少1;
    int count = [_danJia bulletCount];
    [_danJia setBulletCount:–count];
    NSLog(@“剩余子弹数量:%d”,count);
    }
    else
    {
    NSLog(@“子弹都没有,突突突毛线啊。。。”);
    }
    }
    @end
5.Soldier.h


#import
#import “Gun.h”

@interface Soldier : NSObject
{
NSString *_name;
NSString *_type;
Gun *_gun;
}

  • (void)setName:(NSString *)name;

  • (NSString *)name;

  • (void)setType:(NSString *)type;

  • (NSString *)type;

  • (void)setGun:(Gun *)gun;

  • (Gun *)gun;

  • (void)fire;
    @end

6.Soldier.m

#import “Soldier.h”

@implementation Soldier

  • (void)setName:(NSString *)name
    {
    _name = name;
    }

  • (NSString *)name
    {
    return _name;
    }

  • (void)setType:(NSString *)type
    {
    _type = type;
    }

  • (NSString *)type
    {
    return _type;
    }

  • (void)setGun:(Gun *)gun
    {
    _gun = gun;
    }

  • (Gun *)gun
    {
    return _gun;
    }

  • (void)fire
    {
    NSLog(@“预备,开火…”);
    [_gun shoot];
    }
    @end

7.main.m

#import
#import “Soldier.h”
int main()
{
DanJia *dj = [DanJia new];
[dj setMaxCapacity:100];
[dj setBulletCount:50];
Gun *ak47 = [Gun new];
[ak47 setModel:@“Ak47”];
[ak47 setDanJia:dj];
//[ak47 setBulletCount:100];

Soldier *xsd = [Soldier new];
[xsd setName:@"许三多"];
[xsd setType:@"炮兵"];
[xsd setGun:ak47];
for(int i = 0 ; i < 100 ; i++)
    [xsd fire];
return 0;

}

总结

以上这7个文件,分别复制到相应的文件里,运行即可。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011852.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号