我目前正在C#中使用一个非常简单的Pokémon应用程序。我是如何设置的神奇宝贝和他们的战斗类型,但我之间的关系不确定的只是最终做了以下内容:继承口袋妖怪仅在继承时更改值
abstract class Pokemon
{
private int number;
private string name;
protected string weakness;
protected string strength;
public Pokemon(int number, string name)
{
this.number = number;
this.name = name;
weakness = "none";
strength = "none";
}
}
我那么做单独的类只有改变的特定弱点和力量方式。
class Fire : Pokemon
{
public Fire(int number, string name) : base(number, name)
{
weakness = "Water";
strength = "Grass";
}
}
这是不好的礼仪创建子类的唯一目的是简单地改变父母的一些初始值?
从这里开始,我只是打算做的时候,关于如何提高应用程序的应用程序“创造”口袋妖怪
pokemon = new Pokemon[6];
pokemon[0] = new Grass(001, "Bulbasaur");
pokemon[1] = new Grass(002, "Ivysaur");
pokemon[2] = new Grass(003, "Venusaur");
pokemon[3] = new Fire(004, "Charmander");
pokemon[4] = new Fire(005, "Charmeleon");
pokemon[5] = new Fire(006, "Charizard");
的任何和所有的建议和如何使用继承正确是非常赞赏的以下内容: )
另一种可能的解决方案是在构造函数中使用枚举,所以你可以像'PokemonType.Grass'或'PokemonType.Fire'那样传递一些东西。你甚至可以将它设置为接受一个'列表'用于两种类型的宠物小精灵 –
Hill