从超类调用一个不工作Java的方法

时间:2014-03-22 19:41:54

标签: java eclipse swing inheritance

我现在正在开发类似游戏的文字角色扮演游戏。我已将其设置为在Swing窗口中显示不可编辑的JTextPane和JTextField。我使用摇摆,所以我可以有不同的颜色的文字。现在游戏的工作方式是为用户提供多个与数字相关联的选项,他们选择一个数字,游戏处理他们选择的选项。

现在我遇到的问题相对较新。我在超类中有一个方法,应该传递给子类。它开始了战斗序列。

现在,此方法在中间调用另一个名为Decision的方法,该方法为用户提供作为参数传入的选项。这是不起作用的部分。 Decision和Battle都属于超类,但它们在子类中使用。最初,决定不在子类中工作,除非复制到子类本身。现在,Battle只循环一次,在第二个循环中,它就像决定一样停止工作。当我将Battle复制到子类中时,它根本不起作用。

这让我感到困惑的是,无论如何,这种方法在层次结构中的位置都很重要。

这是决策方法

决策方法

public static int decision(String question, int length, String[] choices){
    int[] numbers = new int[length];
    int iterator = 1;
    for(int i = 0; i < length; i++){
        numbers[i] = iterator;
        iterator++;
    }
    boolean done = false;
    while(!done){
        print("");
        print(question);
        String options = "";
        for(int i = 0; i < numbers.length; i++){
            options = (options + numbers[i] + " - " + choices[i] + "  ");
        }
        print(options);
        boolean univSet = true;
        int temp = univInt;
        int entry = 1;
        while(univSet){
            if(univInt != 0){
                univSet = false;
                entry = univInt;
                univInt = 0;
            }
        }
        if(entry == 23){
            help();
        }else{
            for(int i = 0; i < numbers.length; i++){
                if(entry == numbers[i]){
                    done = true;
                    univInt = 0;
                    return entry;
                }
            }
            print("Invalid Number, Try again");
            print("");
            univInt = 0;
        }
    }
    return (Integer) null;
}

BATTLE方法

public static void battle(String name, int health, int power){
    //print("-----------");
    int iHealth = player.health;
    sPrint(name + " battle initiated!");
    print("You're pull out your " + player.curWeapon + "!");
    print(player.actionTexts[player.weaponPos]);
    int eHealth = health;
    boolean done = false;
    while(!done){
        sPrint("Equipped Weapon and Power: " + player.curWeapon + " - " + player.weaponPower[player.weaponPos] + "\nCurrent Health: " + player.health + "\nEnemy Health: " + eHealth);
        int move = decision("What will you do?", 3, new String[]{"Attack", "Block", "Cower In Fear"});
        if(move == 1){
            print("You attacked with the " + player.curWeapon + " for " + (int)(player.power * player.powerUp) + " damage!");
            eHealth -= (int)(player.power * player.powerUp);
            print("The " + name + " attacked for " + power + " damage!");
            player.health -= power;
        }else if(move == 2){
            int chance = (int) (Math.random()* 100);
            if(chance < 25){
                print("You successfully blocked!");
                print("The " + name + " attacked for 0 damage!");
            }else if(chance >= 75){
                print("You failed to block!");
                print("The " + name + " attacked for " + power + " damage!");
                player.health -= power;
            }else{
                print("You half blocked!");
                print("The " + name + " attacked for only " + (power/2) + " damage!");
                player.health -= (power/2);
            }
        }else{
            gameOver();
        }
        if(player.health <= 0){
            print("You ran out of health!");
            done = true;
            gameOver();
        }
        if(eHealth <= 0){
            sPrint("You won the battle!");
            print(""); 
            player.health = iHealth;
            done = true;
        }
    }
}

我知道我描述的方式可能非常令人困惑。但我很乐意澄清所有问题。我只是想知道为什么会这样做以及如何解决它。

这是我下载游戏的链接:http://wikisend.com/download/353028/src.rar

如果您想要访问问题的网站,请输入1,&#39;完成&#39;,1,任何文本,1,2,1,然后它会停止工作。或者,您可以直接阅读并玩游戏。感谢愿意提供帮助的任何人!

编辑:

让我最困惑的是这一切在我实施挥杆之前都有效。除了添加这部分决定之外,两种方法中的一切都是相同的

        boolean univSet = true;
    int temp = univInt;
    int entry = 1;
    while(univSet){
        if(univInt != 0){
            univSet = false;
            entry = univInt;
            univInt = 0;
        }
    }

univInt是一个来自框架类的静态整数,在通过决策时设置为0,否则它是在摆动显示中输入的任何数字。决定的问题是,有时,它会随机地说univInt == 0,当我给出的每个测试都说不然。

编辑2:

我已将所有静态引用更改为子类中最高的实例,然后在子类中,它们都在eclipse中显示错误,建议的修复方法是将它们更改为静态。为什么会这样?具体来说,打印方法不起作用,它将传递给JTextPane的文本添加到其中。这是打印方法:

public void print(String s){
    StyleConstants.setForeground(style, Color.black);
     try { 
         doc.insertString(doc.getLength(), s + "\n",style); 
     }
     catch (BadLocationException e){}
     output.select(doc.getLength(),doc.getLength());
}

print函数在它所在的同一个类中工作,但不在下面的类中。

1 个答案:

答案 0 :(得分:1)

您的方法声明为staticstatic方法根本不属于类层次结构。只有实例(非静态)方法。