简体   繁体   中英

How can i set the value of one Class to a other Class?

how can i give : goblin.getLeben(); the value of lebenmonster.roll(); goblin.getLeben(); the value of lebenmonster.roll(); , (this does not work : goblin.getLeben(lebenmonster.getValue()) ).

Or do i have to change the method because i use a int here and lebenmoster.getValue is from the Class Die ?

public int getLeben(int health) {return Leben = generator.nextInt(health);


public class DiesRun {
   public static void main(String[] args) {
       Charakter goblin = new Charakter("Goblin Fred","Destroyer");
       Die schadenmensch = new Die(6);
       Die schadenmonster = new Die(6);
       Die lebenmonster = new Die(10);
       Die lebenmensch = new Die(10);
           lebenmonster.roll();
       System.out.println(goblin.getLeben(lebenmonster.getValue()));
   }
} 
package Rectangle;

import java.util.Random;

public class Charakter {
  public String Name;
  public String Weapon;
   public int Leben;
  Die _leben;
  Die _weapon;
  Random generator;
  public Charakter(String Namen, String Weapon){
      this.Name = Namen;
      this.Weapon = Weapon;
  }

   public int getLeben(int health)  {
       return Leben = generator.nextInt(health);
   }
}
package Rectangle;

import java.util.Random;

public class Die {
    int sides;
     int value;
    Random generator;

    public Die(int sides) {
        this.sides = sides;
        generator = new Random();
        this.value = this.roll();
    }
    public Die(int sides, int seed) {
        this.sides = sides;
        this.generator = new Random(seed);
    }

    public int roll() {
        this.value = generator.nextInt(this.sides) + 1;
        return this.value;
    }

    public int getValue() {
        return this.value;
    }

    public int getSides() {
        return this.sides;
    }
     public void schaden() {
         System.out.printf("Deals Damage : %d \n", getValue());
     }
    public void leben() {
        System.out.printf("Life : %d \n", getValue());
    }
}

Java classes usually have getters (where you retrieve the value) and setters (where you set values).

For example:

public class Die {

   private int leben;

   public int getLeben() {
      return leben;
   }

   public void setLeben(int leben) {
      this.leben = leben;
   } 
}

If you follow this format you will be able to get and set any values from any "monster" to any "monster" :)

good luck!

May be you can try this approach to modified Roll method and Die constructor

public Die(int sides) {
        this.sides = sides;
        generator = new Random();
        this.value = this.roll(this.sides);
}



public int roll(int sides) {
        this.value = generator.nextInt(sides) + 1;
        return this.value;
}

hope it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM