使用外部类中的方法从该方法获取特定变量

时间:2014-11-25 13:55:02

标签: java

我正在用Java编写一个基于文本的基于任务的搜索游戏,以便更好地掌握语言。

我正在尝试创建一个可以从另一个类调用的方法。我想调用的方法包含重复的变量,我希望在使用switch语句时显示正确的变量。

public class Enemies {

    String enemyName = "Generic Enemy";//initialised the enemy stats so something is called if the switch failed
    double enemyHealth = 110.0;
    int enemyAttackDamage = 10;
    int enemyDefenseLevel = 1;
    int hardLevel = 1;
    int lootDropped = 50;

    public void enemyDecided(int choice){
        switch(choice)
        {
            case 1: 
                this.enemyName = "Sentient Shoelace";
                this.enemyHealth = 100.0;
                this.enemyAttackDamage = 1;
                this.enemyDefenseLevel = 1;
                this.hardLevel = 2;
                this.lootDropped = 100;
                break;
            case 2:
                this.enemyName = "Slimey Fartbox";
                this.enemyHealth = 100.0;
                this.enemyAttackDamage = 2;
                this.enemyDefenseLevel = 1;
                this.hardLevel = 3;
                this.lootDropped = 100;
                break;
            default: System.out.println("Mess up in the enemy selection process");
        }
    }
}

我试图通过

从另一个类中调用它
private void fight()
{
    //I'm using a rand.nextInt() to get enemychoice
    Enemies enemyToFight = new Enemies();
    enemy = enemyToFight.enemyDecided(enemyChoice);//
    System.out.println("You run into a "  + enemy.enemyName);
}

我的fight()方法根本不起作用。并且有充分的理由。我写的只是纯粹的废话,虽然我是在我的智慧结束和我微薄知识的边缘。

我确信任何有眼睛的人都能看到我正在尝试构建一个伪struct。作为最近从C转换,我仍然在学习如何做得更好。如果这在Java中是一个非常错误的想法,那么你可以指出我正确的方向。虽然有机会拯救它会让我非常高兴。

但是如果有可能的话,我想尝试让这个工作正常,所以任何帮助都会受到赞赏。

6 个答案:

答案 0 :(得分:3)

您真正想要的是Enemies类的构造函数,而不是void方法:

public Enemies(int choice){
    switch(choice)
    {
    case 1: 
        this.enemyName = "Sentient Shoelace";
        this.enemyHealth = 100.0;
        this.enemyAttackDamage = 1;
        this.enemyDefenseLevel = 1;
        this.hardLevel = 2;
        this.lootDropped = 100;
        break;
    case 2:
        this.enemyName = "Slimey Fartbox";
        this.enemyHealth = 100.0;
        this.enemyAttackDamage = 2;
        this.enemyDefenseLevel = 1;
        this.hardLevel = 3;
        this.lootDropped = 100;
        break;
    default: System.out.println("Mess up in the enemy selection process");
    }
}

现在,您可以按如下方式更改fight()方法:

private void fight()
{
    //I'm using a rand.nextInt() to get enemychoice
    Enemies enemy = new Enemies(enemyChoice);
    System.out.println("You run into a "  + enemy.enemyName);
}

您可能还需要思考一些风格问题:

  1. 您真的希望将其称为Enemy而不是Enemies
  2. 如果enum有两种可能性,您可以考虑使用Enemy
  3. 您可能还会考虑使用另一个构造函数来获取六个参数,每个参数对应您设置的每个参数。如果您愿意,这可以是private构造函数,并且可以由public的{​​{1}}构造函数调用。

答案 1 :(得分:2)

这是一个非常奇怪的实现,非常面向对象。

  • 您应该拥有Enemy课程,其成员目前位于Enemies课程中。

  • 您的Enemies类应包含List<Enemy>变量,其中包含每个Enemy的实例。

然后您的enemyDecided方法就像:

一样简单
public Enemy enemyDecided(int choice) 
{
    return enemies.get(choice);
}

答案 2 :(得分:1)

您缺少来自您正在呼叫的方法的回报。而不是

public void enemyDecided(int choice){

使用

公共敌人敌人决定(选择){

在方法结束时添加return this;

答案 3 :(得分:1)

我不确定你遇到了什么问题,但不管怎么说,我还是会提出我的想法:)

Java中,不建议使用多个类名,因此不是Enemies,而是拥有所有必需属性的Enemy类,以及处理所有内容的控制器。

这是我快速创建的内容,以更面向对象的方式来说明这个想法:

<强>敌人

class Enemy {

    private final String enemyName;
    private final double enemyHealth;
    private final int enemyAttackDamage;
    private final int enemyDefenseLevel;
    private final int hardLevel;
    private final int lootDropped;

    public Enemy(String enemyName, double enemyHealth, int ad, int atd, int edl, int hl, int ld) {
        this.enemyName = enemyName;
        this.enemyHealth = enemyHealth;
        this.enemyAttackDamage = ad;
        this.enemyDefenseLevel = edl;
        this.hardLevel = hl;
        this.lootDropped = ld;
    }

    public String fight(final Player player) {
        // your fight method
        return null;
    }
}

<强>播放器

class Player {
    // stub
}

<强> FightController

class FightController {

    private List<Enemy> enemies;
    private Player player;

    public FightController(List<Enemy> enemies, Player player) {
        this.enemies = enemies;
        this.player = player;
    }

    public void fight() {
        final int choice = 4;// decided by fair dice roll;
        if (choice >= 0 && choice < this.enemies.size()) {
            final Enemy toFight = this.enemies.get(choice);
            final String result = toFight.fight(player);

            // do somthing with result
        }
    }

    public static void main(String[] args) {
        Enemy first = new Enemy("One", 100d, 1, 1, 1, 1, 1);
        Enemy second = new Enemy("second", 100d, 1, 1, 1, 1, 1);
        Enemy third = new Enemy("third", 100d, 1, 1, 1, 1, 1);
        Enemy fourth = new Enemy("fourth", 100d, 1, 1, 1, 1, 1);
        Enemy fifth = new Enemy("fifth", 100d, 1, 1, 1, 1, 1);

        new FightController(Arrays.asList(fifth, second, third, fourth, fifth), null/* your player*/).fight();
    }
}

答案 4 :(得分:1)

这是因为enemyDecided(int choise)无效。 你可以: (i)将其返回this并将返回类型更改为Enemies enemyDecided(int choice) 要么 (ii)仅使用fight方法中的一个变量:

enemyToFight.enemyDecided(enemyChoice);
System.out.println("You run into a " + enemyToFight.enemyName);

答案 5 :(得分:0)

public Enemies enemyDecided(int choice){
switch(choice)
{
case 1: 
    this.enemyName = "Sentient Shoelace";
    this.enemyHealth = 100.0;
    this.enemyAttackDamage = 1;
    this.enemyDefenseLevel = 1;
    this.hardLevel = 2;
    this.lootDropped = 100;
    break;
case 2:
    this.enemyName = "Slimey Fartbox";
    this.enemyHealth = 100.0;
    this.enemyAttackDamage = 2;
    this.enemyDefenseLevel = 1;
    this.hardLevel = 3;
    this.lootDropped = 100;
    break;
default: System.out.println("Mess up in the enemy selection process");
}
return this;

}

Enemies enemy = enemyToFight.enemyDecided(enemyChoice);//
    System.out.println("You run into a "  + enemy.enemyName);