Test--继承封装多态做游戏

//main函数
//
//  main.m
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//
/*
 封装:把所有共性的东西放到一起
 优点:1.代码清晰
      2.模块化方便使用,减少代码量
      3.复用性高 便于维护

 隐藏内部实现
 */
 /*
  分析需求->指定流程图->搭建框架->完善各个功能

  Hero -> Person -> 名字、攻击力、血量、防御力
  Monster -> Person -> 名字、攻击力、血量、防御力

  Hero -> 初始技能、初始武器、初始化装备

  HeroSkill -> Skill -> 名字伤害
  MonsterSkill -> Skill -> 名字伤害

  Weapon -> 名字伤害
  Clothes -> 名字防御
  */
#import <Foundation/Foundation.h>
#import "RequstData.h"
#import "Clothes.h"
#import "Weapon.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {

        RequstData *requst = [[RequstData alloc]init];
        [requst loadData];
        Weapon *weapon = requst.weaponList[0];
        NSLog(@"%@ %d",weapon.name,weapon.att);


    }
    return 0;
}
//Clothes类 .h文件
//
//  Clothes.h
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Clothes : NSObject

@property NSString *name;   //衣服的属性
@property int def;

@end 


//.m文件
//
//  Clothes.m
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "Clothes.h"

@implementation Clothes

- (instancetype)initWithName:(NSString *)name andDef:(int)def         //自定义构造函数,可以给cloth对象初始化内容
{
    self = [super init];
    if (self) {
        _name = name;
        _def = def;
    }
    return self;
}

@end
//Weapon类   .h文件
//
//  Weapon.h
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Weapon : NSObject

@property NSString *name;
@property int att;

@end


//.m文件
//
//  Weapon.m
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "Weapon.h"

@implementation Weapon

- (instancetype)initWithName:(NSString *)name andAtt:(int)att
{
    self = [super init];
    if (self) {
        _name = name;
        _att = att;
    }
    return self;
}
@end
//Skill总类,包含技能的name,att属性,则HeroSkill类和MonsterSkill类就可以直接继承Skill类,而可以不用做初始化直接用
// .h文件

//  Skill.h
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Skill : NSObject

@property NSString *name;
@property int att;

@end


//.m文件
//
//  Skill.m
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "Skill.h"

@implementation Skill

- (instancetype)initWithName:(NSString *)name andAtt:(int)att
{
    self = [super init];
    if (self) {
        _name = name;
        _att = att;
    }
    return self;
}

@end
//这里可以做一个Person总类,包含英雄和怪物的几个相同的基本属性
//.h文件
//
//  Person.h
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
//名字,攻击力,血量,防御力
@property NSString *name;
@property int att;
@property int blood;
@property int def;

@end

//.m文件不用写什么,默认就行
//Hero类  .h文件

//
//  Hero.h
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "Person.h"
@class  Skill;
@class  Weapon;
@class  Clothes;

@interface Hero : Person

@property Skill *skill;  //定义英雄技能
@property Weapon *weapon;    //定义英雄武器
@property Clothes *clothes;  //定义英雄衣服

- (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)heroSkill andWeapon:(Weapon *)heroWeapon andCloth:(Clothes *)heroCloth;

@end


//.m实现文件

//  Hero.m
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "Hero.h"
#import "Skill.h"
#import "Weapon.h"
#import "Clothes.h"
@implementation Hero

- (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)heroSkill andWeapon:(Weapon *)heroWeapon andCloth:(Clothes *)heroCloth
{
    self = [super init];
    if (self) {
        self.name = name;
        self.blood = blood;
        self.att = att;
        self.def = def;
        _skill = heroSkill;
        _clothes = heroCloth;
        _weapon = heroWeapon;
    }
    return self;
}

@end
//Monster类  .h文件
//
//  Monster.h
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "Person.h"
@class Skill;
@interface Monster : Person

@property Skill *monsterSkill;

- (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)monsterSkill;

@end


//.m文件
//
//  Monster.m
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "Monster.h"
#import "Skill.h"
@implementation Monster

- (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)monsterSkill
{
    if (self) {
        self.name = name;
        self.blood = blood;
        self.att = att;
        self.def = def;
        _monsterSkill = monsterSkill;

    }
    return self;
}

@end
//程序加载数据,执行操作的类
//RequstData类  .h文件
//
//  RequstData.h
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface RequstData : NSObject
{
    NSArray *clothesData;
    NSArray *weaponData;
    NSArray *heroSkillData;
    NSArray *monsterSkillData;
    NSArray *heroData;
    NSArray *monsterData;
}

/*
    nonatomic(非原子性) 是不在多线程中,非原子性不保证数据的安全(只要不是在多线程的情况下,就是用nonatomic)
    natomic(原子性) 可以在多线程中保证数据的安全

 */

@property (nonatomic)NSMutableArray *clothesList;
@property (nonatomic)NSMutableArray *weaponList;
@property (nonatomic)NSMutableArray *heroSkillList;
@property (nonatomic)NSMutableArray *MonsterSkillList;
@property (nonatomic)NSMutableArray *HeroList;
@property (nonatomic)NSMutableArray *monsterList;


-(void)loadData;
-(NSMutableArray *)weaponList;   //main函数中使用w武器时要用的方法,此方法可以得到具体的weapon对象以及所包含的具体内容,以下方法雷同
-(NSMutableArray *)heroList;
-(NSMutableArray *)monsterList;
-(NSMutableArray *)heroSkillList;
-(NSMutableArray *)clothesList;
-(NSMutableArray *)MonsterSkillList;

@end


 //RequstData类的具体实现方法内容  .m文件
 //
//  Requstbaga.m
//  NoteOC-老师Game
//
//  Created by 刘佳斌 on 15/11/25.
//  Copyright © 2015年 刘佳斌. All rights reserved.
//

#import "RequstData.h"
#import "NSDictionary+ReadFile.h"
#import "Clothes.h"
#import "Weapon.h"
#import "Skill.h"
#import "Hero.h"
#import "Monster.h"
@implementation RequstData

-(void)loadData{

    NSDictionary *allData = [NSDictionary readFileWithName:@"GameData"];
    //NSLog( @"%@",allData);

    //获取所有数据allData里面的clothes等key对应的数据内容
    clothesData = allData[@"clothes"];
    weaponData = allData[@"weapon"];
    heroSkillData = allData[@"heroSkill"];
    monsterSkillData = allData[@"monsterSkill"];
    heroData = allData[@"hero"];
    monsterData = allData[@"monster"];


    //当打印数组字典或者其他对象,结果是null的时候
    //1.有没有初始化 创建这个对象
    //2.如果创建了也是null,查找是否有地方给这个对象赋值null
    //NSLog(@"%@",self.clotheslist);


//懒加载  在需要的时候才去加载,不需要的时候不加载  可以在getter方法里实现
}

#pragma mark -----导入武器
-(NSMutableArray *)weaponList{

    _weaponList = [NSMutableArray array];
    for (NSDictionary *weaponDic in weaponData) {
        Weapon *weapon = [[Weapon alloc]init];
        weapon.name = weaponDic[@"name"];
        weapon.att = [weaponDic[@"att"] intValue];
        [_weaponList addObject:weapon];
    }
    return _weaponList;

}


#pragma mark -----导入衣服
-(NSMutableArray *)clothesList{

    if (_clothesList!=nil) {
        return _clothesList;
    }
    _clothesList = [NSMutableArray array];
    for (NSDictionary *clothDic in clothesData) {
        Clothes *cloth = [[Clothes alloc]init];
        cloth.name = clothDic[@"name"];
        cloth.def = [clothDic[@"def"]intValue];
        [_clothesList addObject:cloth];
    }
    return _clothesList;
}


#pragma mark  ------英雄技能--
-(NSMutableArray *)heroSkillList{

    _heroSkillList = [NSMutableArray array];
    for (NSDictionary *heroSkillDic in heroSkillData) {
        Skill *heroSkill = [[Skill alloc]init];
        heroSkill.name = heroSkillDic[@"name"];
        heroSkill.att = [heroSkillDic[@"att"] intValue];
        [_heroSkillList addObject:heroSkill];
    }
    return _heroSkillList;
    }


#pragma mark   ----怪物技能

-(NSMutableArray *)MonsterSkillList{

    _MonsterSkillList = [NSMutableArray array];
    for (NSDictionary *monsterSkillDic in monsterSkillData) {
        Skill *monsterSkill = [[Skill alloc]init];
        monsterSkill.name = monsterSkillDic[@"name"];
        monsterSkill.att = [monsterSkillDic[@"att"] intValue];
        [_MonsterSkillList addObject:monsterSkill];
    }
    return _MonsterSkillList;
}

#pragma mark   ---英雄

-(NSMutableArray *)heroList{

    _HeroList = [NSMutableArray array];
    for (NSDictionary *heroListDic in heroData) {
        Hero *hero = [[Hero alloc]initWithName:heroListDic[@"name"] andBlood:[heroListDic[@"blood"]intValue] andAtt:[heroListDic[@"att"]intValue] andDef:[heroListDic[@"def"]intValue] andSkill:_HeroList[0] andWeapon:_weaponList[0] andCloth:_clothesList[0]];
        [_HeroList addObject:hero];
        }
    return _HeroList;
}

#pragma mark   ---怪物

-(NSMutableArray *)monsterList{

    _monsterList = [NSMutableArray array];
    for (NSDictionary *monsterDic in monsterData) {
        Monster *monster = [[Monster alloc]initWithName:monsterDic[@"name"] andBlood:[monsterDic[@"blood"]intValue] andAtt:[monsterDic[@"att"]intValue] andDef:[monsterDic[@"def"]intValue] andSkill:_monsterList[0]];
        [_monsterList addObject:monster];
    }
    return _monsterList;
}


@end
以下是一个关于Java封装继承多态的例子: ```java // 父类Animal public class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public void eat() { System.out.println(name + " is eating."); } } // 子类Dog public class Dog extends Animal { public Dog(String name) { super(name); } public void bark() { System.out.println(getName() + " is barking."); } } // 子类Cat public class Cat extends Animal { public Cat(String name) { super(name); } public void meow() { System.out.println(getName() + " is meowing."); } } // 测试类 public class Test { public static void main(String[] args) { Animal animal1 = new Dog("Lucky"); Animal animal2 = new Cat("Tom"); animal1.eat(); animal2.eat(); // animal1.bark(); // 编译错误,因为Animal类没有bark()方法 // animal2.meow(); // 编译错误,因为Animal类没有meow()方法 Dog dog = (Dog) animal1; Cat cat = (Cat) animal2; dog.bark(); cat.meow(); } } ``` 在这个例子中,Animal类是一个父类,它有一个私有的name属性和一个公共的eat()方法。Dog和Cat类是Animal类的子类,它们继承了父类的属性和方法,并且分别有自己的方法bark()和meow()。 在测试类中,我们首先创建了一个Dog对象和一个Cat对象,并且调用它们的eat()方法。由于Animal类没有bark()和meow()方法,我们不能直接调用这些方法。但是我们可以将animal1和animal2分别强制转换为Dog和Cat类型的对象,这样就可以调用它们的特有方法。 这个例子展示了Java中封装继承多态的特性。封装指的是将属性和方法封装在类中,只对外暴露必要的接口。继承指的是子类继承父类的属性和方法,并且可以在此基础上添加自己的属性和方法。多态指的是同一个方法在不同的对象中有不同的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值