我应该使用多个类游戏吗? [英] Should I use multiple classes for game?

查看:91
本文介绍了我应该使用多个类游戏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在PHP中制作一个基于文本的RPG类型的程序,作为一个假日项目,并有机会了解更多关于PHP和OOP。 (也许不是语言中最好的选择,我知道,但我不想在OOP的同时从头开始学习另一种语言。)

I'm considering making a text-based RPG-type program in PHP as both a holiday project and a chance to learn more about PHP and OOP. (Maybe not the best choice in language, I know, but I didn't want to have to learn another language from scratch at the same time as OOP.)

,我只是开始设计过程和思考怪物。每个怪物类型(你知道,orc,地精,老鼠等)将有自己的统计,技能和什么不。起初我虽然我可以只有一个单一的怪物类,并设置属性,当我实例化对象。但是我想这可能是一个低效率,所以我正在考虑为每个类型的怪物一个类。

Anyway, I'm just beginning the design process and thinking about 'monsters'. Each monster type (ya know, orc, goblin, rat, etc) will have its own stats, skills and what not. At first I though I could just have a single monster class and set the properties when I instantiate the object. But then I figured that might be a little inefficient, so I'm considering having a class for each type of monster.

最好的方法来处理问题,考虑到每个类中的方法可能会是一样的?有没有更好的方式来做我还不知道的事情。

Is this the best way to approach the problem, considering that the methods in each class will likely be the same? Is there a better way of doing things that I don't yet know about?

任何帮助是值得赞赏的。

Any help is appreciated.

推荐答案

你可以创建一个抽象类,例如Monster,然后为每个不同类型的怪物扩展该类。

You could create an abstract class, say Monster, and then extend that class for each of the different types of monsters. So

<?php
abstract class Monster {
  private $health;
  public function attack() {
    //do stuff
  }
}
class Orc extends Monster {
  private $damage_bonus;
}
?>

编辑
Orc将扩展Monster,然后继承属性$ health和function attack()。

edit Orc would extend Monster and then inherit the attribute $health and the function attack().

这篇关于我应该使用多个类游戏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆