gpt4 book ai didi

java - 我的对象类到武器类和装甲类的继承是什么

转载 作者:行者123 更新时间:2023-11-30 04:11:39 26 4
gpt4 key购买 nike

我正在制作一个小游戏,主角的库存里装满了物体。

public enum Objects_type 
{
WEAPON,
ARMOR
}

public abstract class Objects_class
{
protected String name;
protected Objects_type type;

public Objects_class(String name, Objects_type type)
{
this.name = name;
this.type = type;
}
}

public abstract class Armor extends Objects_class{

int life = 0;
int res_fire = 0;

public Armor(String name, int largeur, int hauteur) {
super(name, Objects_type.ARMOR);
}
}

public abstract class Weapon extends Objects_class
{
protected int dmg_fire = 0;

public Weapon(String name) {
super(name, Objects_type.WEAPON);
}
}

public class StickOfJoy extends Weapon{

public StickOfJoy() {
super("Stick of Joy");
dmg_fire = 2;
}
}

public class ArmorOfPity extends Armor{
public ArmorOfPity()
{
super("Armor of Pity");
life = 30;
}
}

然后我有这样的功能:

Hero.getObject (Objects_class obj)
{
if (obj.getType == Objects_type.WEAPON)
....
}

我希望能够将 Objects_class obj 视为武器,但当然这是不可能的(将母亲转换到其 child ),因此这让我认为我的继承结构很糟糕。

我应该做什么?

最佳答案

大卫·康拉德 (David Conrad) 有一些很好的观点,我建议您仔细阅读,我不会在这里重复,但我会这样做。

假设您有一个角色在游戏世界中漫游并拾取元素,可能有许多不同的元素,其中一些元素的行为彼此不同,因此需要创建一个新的子类(例如拾取 Boot 与拾取)向上翅膀)。

一旦你拿起一个元素,你可以选择让英雄尝试看看拿起了什么类型的元素(实例,枚举,等等),或者你可以让元素弄清楚它应该去哪里。

这是一个简化的示例,其中玩家只有两个库存槽,一个武器和一个盔甲。请注意,只需将新元素(例如生命药水或 super 新特殊武器)添加到组合中是多么容易,而无需更改播放器中的任何内容或进行类型转换。

public abstract class Item {
private int ID;
private static int IDCounter;
private String name;

public Item(String name) {
this.name = name;
this.ID = IDCounter;
IDCounter++;
}

public int getID() {
return ID;
}

public String getName() {
return name;
}

public abstract void attachToPlayer(Player player);
}

public class Armor extends Item {
private int life;
private int res_fire;

public Armor(String name) {
super(name);
}

@Override
public void attachToPlayer(Player player) {
// Only equip if upgrade
if (player.getArmor().res_fire > this.res_fire)
player.setArmor(this);

}

}


public class Weapon extends Item {
private int dmg_fire;

public Weapon(String name) {
super(name);
}

// ...stuff

@Override
public void attachToPlayer(Player player) {
// Only equip this if upgrade? You decide the logic
if(player.getWeapon().dmg_fire>this.dmg_fire)
player.setWeapon(this);
}

}

public class SuperSpecialWeapon extends Weapon {
private float bonusHealthModifier = 1.0f;
public SuperSpecialWeapon(String name) {
super(name);
}

@Override
public void attachToPlayer(Player player) {
// This bonus adds +100%HP bonus to the player!
int hp = (int) ((1 + bonusHealthModifier) * player.getHealth());
player.setHealth(hp);
player.setWeapon(this);
}

}

public class Potion extends Item {
private int health = 100;

public Potion() {
super("HealthPotion");
}

@Override
public void attachToPlayer(Player player) {
// If the player has room for one more potion, pick this up
Potion[] potions = player.getHealthPotions();
for (int i = 0; i < potions.length; i++) {
if(potions[i]==null){
potions[i] = this;
break;
}
}
}

// ..other stuff
}

最后是播放器

public class Player {
private Armor armor;
private Weapon weapon;
private String name;
private Potion[] healthPotions = new Potion[10];
private int health;

public Player(String name) {
this.name = name;
}

public Armor getArmor() {
return armor;
}

public Weapon getWeapon() {
return weapon;
}

public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}

public void setArmor(Armor armor) {
this.armor = armor;
}

public void setHealth(int health) {
this.health = health;
}

public int getHealth() {
return health;
}

public Potion[] getHealthPotions() {
return healthPotions;
}

}

关于java - 我的对象类到武器类和装甲类的继承是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19469009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com