Java静态/非静态方法

时间:2015-10-26 16:01:03

标签: java methods

我开始研究我的第一个java项目,这是一个基本的RPG,我有一个关于法术的问题。我有一个名为Character的抽象类,它由一些子类扩展(如FighterMage等)。只有施法者可以施放法术。关于法术 - 我有一个名为Spell的类,它描述了一个咒语(它的名字,它的效果,法术力等)。所有法术都存储在SpellsList类中,该类具有spellsList列表(类Spell的对象)。我有一个Effect类(非常合理,它将成为一个界面)有一些效果,如" damage"并且"治愈"但是我不能同时使用它,我只想测试我的工作。

我的问题是Mage的方法addToSpellBookshowSpellBook给出了编译错误:java can't find symbol: method addToSepllBook, location: variable hero of type Game.Character。也适用于showSpellBook。为什么以及如何解决它? (问题可能出在Mage / Spell / SpellsList课程,而不是Character这个很长,所以它不那么令人生畏:))

public class Game {
public static void main(String[] args) throws IOException {

    CharacterCreator heroCreator = new CharacterCreator();
    CharacterCreator.showAllClasses();

    Scanner sc = new Scanner(System.in);
    int scan = sc.nextInt();
    String chosenClass = CharacterCreator.getCharacterClass(scan);
    Character hero = CharacterCreator.createCharacter(chosenClass);
    try {
        hero.displayCharacter();
    }catch (Exception e){
        System.out.println("Wrong input");
    }




    if (hero.getCharacterClass().equals("Mage")){
        hero.addToSpellBook("Fireball");
        hero.showSpellBook();
    }



}

   }

public class CharacterCreator {

public static Character createCharacter(String chosenClass) {

    Character hero = null;

    System.out.println("Choose Name:");
    Scanner nameIn = new Scanner(System.in);
    String name = nameIn.next();


        switch (chosenClass) {
            case "Fighter":
                return new Fighter(name);
            case "Rogue":
                return new Rogue(name);
            case "Mage":
                return new Mage(name);
            case "Cleric":
                return new Cleric(name);
            case "def":
                System.out.println("Wrong input");
                return null;
            default:
                return null;
        }


}

public static void  showAllClasses(){
    System.out.println("Choose a character: ");
    System.out.println("1. Fighter");
    System.out.println("2. Rogue");
    System.out.println("3. Mage");
    System.out.println("4. Cleric");
}


public static String getCharacterClass(int scan){

    String classIn;

    switch (scan) {
        case 1:
            classIn = "Fighter";
            break;
        case 2:
            classIn = "Rogue";
            break;
        case 3:
            classIn = "Mage";
            break;
        case 4:
            classIn = "Cleric";
            break;
        default:
            System.out.println("Enter again");
            classIn = "def";
    }

    return classIn;
}

}


abstract public class Character {

    private String name;
    private String characterClass;
    private int level;
    private int hp;
    private int currentHp;
    private int armorClass;

    private long xp;
    /*private int BAB; /*Base attack bonus*/

    private int strength;
    private int constitution;
    private int dexterity;
    private int intelligence;
    private int wisdom;
    private int charisma;


    protected Character(String name){

        setName(name);
        setCharacterClass("Class");
        setLevel(1);
        setStrength(10);
        setConstitution(10);
        setDexterity(10);
        setIntelligence(10);
        setWisdom(10);
        setCharisma(10);
        setHp(0);
        setCurrentHp(getHp());
        setArmorClass(10);
        setXp(0);

    }


    void displayCharacter() throws IOException{
        System.out.print("\n\n\n");
        System.out.println("Name: " + getName());
        System.out.println("Class: " + getCharacterClass());
        System.out.println("Level: " + getLevel());
        System.out.println("HP: " + getHp());
        System.out.println("Current HP: " + getCurrentHp());
        System.out.println("Armor Class: " + getArmorClass());

        System.out.println("***************");
        System.out.println("Attributes: ");
        System.out.println("Strength: " + getStrength());
        System.out.println("Constitution: " + getConstitution());
        System.out.println("Dexterity: " + getDexterity());
        System.out.println("Intelligence: " + getIntelligence());
        System.out.println("Wisdom: " + getWisdom());
        System.out.println("Charisma: " + getCharisma());
        System.out.println("***************");
        System.out.println("XP: " + getXp());

    }

    public int getModifier(int number){
        int mod = (int)((number -10)/2);
        return mod;
    }

    public String getName() { return name; }
    public String getCharacterClass() { return characterClass; }
    public int getLevel() { return level; }
    public int getHp() { return  hp; }
    public int getCurrentHp() { return  currentHp; }
    public int getArmorClass() { return  armorClass; }
    public int getStrength(){ return strength; }
    public int getConstitution(){ return constitution; }
    public int getDexterity(){ return dexterity; }
    public int getIntelligence(){ return intelligence; }
    public int getWisdom(){ return wisdom; }
    public int getCharisma(){ return charisma;}
    public long getXp(){ return xp;}


    protected void setName(String Name) { name = Name; }
    protected void setCharacterClass(String characterClass) { this.characterClass = characterClass; }
    protected void setLevel(int lvl){ level = lvl; }
    protected void setHp(int hitPoints){ hp = hitPoints; }
    protected void setCurrentHp(int curHp){ currentHp = curHp; }
    protected void setArmorClass(int ac){ armorClass = ac; }
    protected void setStrength(int str){ strength = str; }
    protected void setConstitution(int con){ constitution = con; }
    protected void setDexterity( int dex) { dexterity = dex; }
    protected void setIntelligence(int intel){ intelligence = intel; }
    protected void setWisdom(int wis){ wisdom = wis; }
    protected void setCharisma(int cha){charisma = cha; }
    protected void setXp(int XP){xp = XP; }

    }

public class Mage extends Character  {

private List<Spell> spellBook;

public Mage(String name){

    super(name);

    setName(name);
    setCharacterClass("Mage");
    setLevel(1);
    setStrength(10);
    setConstitution(10);
    setDexterity(14);
    setIntelligence(16);
    setWisdom(14);
    setCharisma(10);
    setHp((int) (4 + getModifier(getConstitution())));
    setCurrentHp(getHp());
    setArmorClass(10 + getModifier(getDexterity()));
    spellBook = null;

}

 void addToSpellBook(String spellName){

    Spell newSpell;
    newSpell = SpellsList.getSpell(spellName);
    spellBook.add(newSpell);

}

void showSpellBook(){

    for (Iterator<Spell> iter = spellBook.iterator(); iter.hasNext(); ) {
        Spell spell = iter.next();
        if (spellBook.equals(spell.getSpellName())) {
            System.out.println("Spell name: " + spell.getSpellName());
            System.out.println("Spell effect: " + spell.getEffect());
        }
        }
}

}



public class Spell {
private String name;
private int spellLevel;
private String effect;
private int manaCost;
private int duration;

Spell(String name, int spellLevel, String effect, int manaCost, int duration){
    this.name = name;
    this.spellLevel = spellLevel;
    this.effect = effect;
    this.manaCost = manaCost;
    this.duration= duration;
}


void castSpell(String spellName, Character hero, Character target){
    try {
        Spell spell = SpellsList.getSpell(spellName);
        System.out.println("You casted: " + spellName);
        System.out.println("Spell effect: " + spell.effect);
    }
    catch (Exception e){
        System.out.println("No such spell");
    }
}

String getSpellName(){ return name; }

int getSpellLevel() {return spellLevel; }

String getEffect(){ return effect; }

int getManaCost(){
    return manaCost;
}

int getDuration() { return  duration; }
}


public class SpellsList {
static List<Spell> spellsList = new ArrayList<Spell>();
static

{
    spellsList.add(new Spell("Fireball", 3, "damage", 5,0 ));
    spellsList.add(new Spell("Ice Storm", 4, "damage", 8, 0));
    spellsList.add(new Spell("Heal", 2, "heal", 4, 0));
}

static Spell getSpell(String spellName) {
    try {
        for (Iterator<Spell> iter = spellsList.iterator(); iter.hasNext(); ) {
            Spell spell = iter.next();
            if (spellName.equals(spell.getSpellName())) {
                return spell;
            }
        }

    } catch (Exception e){
        System.out.println(spellName + " haven't been found in spells-list");
        return null;
    }
    return null;

}

}

3 个答案:

答案 0 :(得分:1)

英雄是Character类型。你应该把它投给法师。或者在Character类中添加addToSpellBook并在Mage类中覆盖它。类似的东西:

if(hero instanceof Mage)
   ((Mage) hero).addToSpellBook();

答案 1 :(得分:0)

您在课程addToSpellBook上调用了代码showSpellBookCharacter,但您的班级Character中没有包含此名称的方法。

答案 2 :(得分:0)

编译时的变量类型与执行期间的类有所不同。问题是你的英雄变量不是Mage类型,而是Character类型,只能访问任何Character可用的方法。

在尝试确保heroMage的实例时,编译器也没有注意到您的逻辑。要告诉它你知道你有Mage并希望使用你需要施放的Mage方法。

您的验证方式没问题,但我建议您使用instanceof关键字。

if(hero instanceof Mage) {
   ((Mage)hero).addToSpellBook("Fireball");
   ((Mage)hero).showSpellBook();
}