【问题标题】:How to call toString() Method from one class to another class without main method in javajava中如何在没有main方法的情况下从一个类调用toString()方法到另一个类
【发布时间】:2022-06-18 00:58:49
【问题描述】:

我正在尝试制作简单的口袋妖怪游戏。

如何将toString 方法从Trainer.java 调用到Arena.java?我尝试使用this.trainer1.toString(),但它不起作用。 Arena.java 也没有 main 方法。

这是文件

Trainer.java

import java.util.Random;

public class Trainer {

    private String nameTrainer;
    private Taschenmonster[] inventar = new Taschenmonster[6];

    // getter and setter Methods for attributes

    /*
     * getter and setter for nameTrainer
     */
    public String getNameTrainer() {
        return this.nameTrainer;
    }
    public void setNameTrainer(String nameTrainer) {
        this.nameTrainer = nameTrainer;
    }

    /*
     * getter and setter for inventar
     */
    // public Taschenmonster getInventar() {
    //     return this.inventar;
    // }
    // public void setName(String nameTrainer) {
    //     this.nameTrainer = nameTrainer;
    // }

    // constructor of class Trainer
    Trainer (String nameTrainer) {
        this.nameTrainer = nameTrainer;
    }

    /*
     * Adds Taschenmonster to Inventar of Trainer.
     */
    public void receive(int pos, Taschenmonster monsterObject) {
       if (pos < 6) {
           inventar[pos] = monsterObject;
       } 
    }

    /*
     * give out a randomly selected Taschenmonster for battle
     */
    public Taschenmonster send() {

        int random = new Random().nextInt(inventar.length);

        return inventar[random];

    }

    /*
     * Overriding the string method 
     */
    @Override
    public String toString() {
        return "Taschenmonster von " + nameTrainer + "\n" + inventar[0].getNameMonster() + " " + inventar[1].getNameMonster() + " " + inventar[2].getNameMonster() + " " + inventar[3].getNameMonster() + " " + inventar[4].getNameMonster() + " " + inventar[5].getNameMonster();    
    }

    
}

Arena.java

public class Arena {


    private Trainer trainer1;
    private Trainer trainer2;

    // constructor of class Arena
    Arena (Trainer trainer1, Trainer trainer2) {
        this.trainer1 = trainer1;
        this.trainer2 = trainer2;
    }

    // giving out the profile of the trainers
    // this.trainer1.toString();
    // this.trainer2.toString();


}

Main.java

public class Main {
    public static void main(String args[]) {

        // making objects from class Taschenmonster
        Taschenmonster t1 = new Taschenmonster("Pikachu", 5, 7);
        Taschenmonster t2 = new Taschenmonster("Eevee", 5, 6);
        Taschenmonster t3 = new Taschenmonster("Charmander", 5, 4);
        Taschenmonster t4 = new Taschenmonster("Bulbasaur", 5, 5);
        Taschenmonster t5 = new Taschenmonster("Squirtle", 5, 2);
        Taschenmonster t6 = new Taschenmonster("Mewoth", 5, 3);

        // instantiating trainers from class Trainer
        Trainer trainer1 = new Trainer("Paul");
        Trainer trainer2 = new Trainer("Chris");

        // adding Taschenmonster to both trainers
        trainer1.receive(0, t1);
        trainer1.receive(1, t2);
        trainer1.receive(2, t3);
        trainer1.receive(3, t4);
        trainer1.receive(4, t5);
        trainer1.receive(5, t6);

        trainer2.receive(0, t1);
        trainer2.receive(1, t2);
        trainer2.receive(2, t3);
        trainer2.receive(3, t4);
        trainer2.receive(4, t5);
        trainer2.receive(5, t6);

        // giving out profiles of Players
        System.out.println(trainer1);
        System.out.println(trainer2);


    }
}

Taschenmonster.java

import java.util.*;

public class Taschenmonster {


    // class attributes
    private String nameMonster;
    private int lives, damageValue;

    // getter and setter Methods for attributes

    /*
     * getter and setter for nameMonster
     */
    public String getNameMonster() {
        return this.nameMonster;
    }
    public void setNameMonster(String nameMonster) {
        this.nameMonster = nameMonster;
    }

    /*
     * getter and setter for lives
     */
    public int getLives() {
        return this.lives;
    }
    public void setLives(int lives) {
        this.lives = lives;
    }

    /*
     * getter and setter for damageValue
     */
    public int getDamageValue() {
        return this.damageValue;
    }
    public void setDamageValue(int damageValue) {
        this.damageValue = damageValue;
    }


    // constructor of class Taschenmonster
    Taschenmonster (String nameMonster, int lives, int damageValue) {
        this.nameMonster = nameMonster;
        this.lives = lives;
        this.damageValue = damageValue;
    }


    /*
     * how much lives is remaining with Taschenmonster
     */
    public void receiveDamage() {
        this.lives--;
        if (this.lives == 0) {
            System.out.println("Caution! No lives left.");
        }
    }

    /*
     * gives out estimated attackValue of Taschenmonster in form of double. 
     */
    public int attackValue() {
        Random random = new Random();
        return this.damageValue *= random.nextInt() % 2;
    }

    /*
     * Overriding toString() Method and values of object 
     */
    @Override
    public String toString() {
        return nameMonster + " " + lives + " " + damageValue;
    }


}

【问题讨论】:

  • 什么是“不起作用”请解释一下?

标签: java


猜你喜欢
  • 1970-01-01
  • 2013-10-08
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多