简体   繁体   中英

Getting instance variable from one class using a method in another class?

How would I get the instance variable hitpoints from the Dog class and pass them to the Lion Class through the method eat(X x)?

I'm trying to get the Lion to eat() the Dog and minus points from the instance variable which is stored in a new variable in the Lion Class.

Class Lion

package helloworld;

public class Lion {
public String name;
public int heightCMeters;
public int lengthCMeters;
public float weightKilos;
public int hitPoints;

public Lion(int hitPoints, String name, int heightCMeters, int lengthCMeters, float weightKilos) {
    this.name = name;
    this.heightCMeters = heightCMeters;
    this.lengthCMeters = lengthCMeters;
    this.weightKilos = weightKilos;
}
public void lionDetails() {
    System.out.println("Name: " + this.name);
    System.out.println("Height CM: " + this.heightCMeters);
    System.out.println("Length CM: " + this.lengthCMeters);
    System.out.println("Weight Kilos: " + this.weightKilos);
}
public void eat(X x) {
    int hitPoints = x.hitPoints  - 10;
    System.out.println(x)
}
}

Class Dog

package helloworld;

public class Dog {
public String name;
public int heightCMeters;
public int lengthCMeters;
public float weightKilos;
public int hitPoints;

public Dog(int hitPoints, String name, int heightCMeters, int lengthCMeters, float weightKilos) {
    this.name = name;
    this.heightCMeters = heightCMeters;
    this.lengthCMeters = lengthCMeters;
    this.weightKilos = weightKilos;
}
public void dogDetails() {
    System.out.println("Name: " + this.name);
    System.out.println("Height CM: " + this.heightCMeters);
    System.out.println("Length CM: " + this.lengthCMeters);
    System.out.println("Weight Kilos: " + this.weightKilos);
}
public void eat(X x) {
    int hitPoints = x.hitPoints - 10;
    System.out.println(x)

}
}

Basically, Lions can eat dogs and the converse is true (which is weird, a dog is not brave enough to attack Lions). Anyways, what you need is an abstract class that represents animals that eat animals, this class should contain the hitPoint you mentioned.

abstract class X {
 public int hitPoints; 
}
// Lions are edible
class Lion extends X{

  public void eat(X x) { // pass an edible object
  int hitPoints = x.hitPoints  - 10;
  System.out.println(x)
  }
}  

//Dogs are edible as well
class Dog extends X{

 public void eat(X x) { // pass an edible object
  int hitPoints = x.hitPoints  - 10;
  System.out.println(x)
  }
}

And now, for a Lion to a eat dog,

Lion predator = new Lion();
Dog prey = new Dog(); 
predators.eat(prey); // this passed dog will be eaten 

Best way write a test class or write main method for Lion class which will maintain hitpoints of both the classes.

 class Test{

    public static void main(String[] args){

         Dog puppy=new Dog(10,"Moti",12,12,31);
         Lion oldLion=new Lion(20,"Old Lion",12,12,43);

         oldLion.eat(puppy);

    }

 }

You must have an abstract class Animal , with all the common methods defined there.

For eat method of Lion class,

public void eat (Animal animal) {
    this.hitPoints-=animal.hitPoints;
}

For eat method of Dog class, also the same logic.

I would make anything that can be eaten implement an interface Edible . Then that interface can have a method isEaten that takes the hit point deduction.

Something like this:

public interface Edible {
void isEaten(final int hitPointsToDeduct);
}

Then your Lion and Dog would implement this so that they could be eaten.

The Dog class would be:

public class Dog implements Edible {

    public String name;
    public int heightCMeters;
    public int lengthCMeters;
    public float weightKilos;
    public int hitPoints;

    public Dog(final int hitPoints, final String name, final int heightCMeters, final int lengthCMeters, final float weightKilos) {
        this.name = name;
        this.heightCMeters = heightCMeters;
        this.lengthCMeters = lengthCMeters;
        this.weightKilos = weightKilos;
    }

    public void dogDetails() {
        System.out.println("Name: " + this.name);
        System.out.println("Height CM: " + this.heightCMeters);
        System.out.println("Length CM: " + this.lengthCMeters);
        System.out.println("Weight Kilos: " + this.weightKilos);
    }

    public void eat(final Edible x) {
        x.isEaten(10);
        System.out.println(x);
    }

    public void isEaten(final int hitPointsToDeduct) {
        this.hitPoints = this.hitPoints - hitPointsToDeduct;
    }
}

And the Lion class:

public class Lion implements Edible {

    public String name;
    public int heightCMeters;
    public int lengthCMeters;
    public float weightKilos;
    public int hitPoints;

    public Lion(final int hitPoints, final String name, final int heightCMeters, final int lengthCMeters, final float weightKilos) {
        this.name = name;
        this.heightCMeters = heightCMeters;
        this.lengthCMeters = lengthCMeters;
        this.weightKilos = weightKilos;
    }

    public void lionDetails() {
        System.out.println("Name: " + this.name);
        System.out.println("Height CM: " + this.heightCMeters);
        System.out.println("Length CM: " + this.lengthCMeters);
        System.out.println("Weight Kilos: " + this.weightKilos);
    }

    public void eat(final Edible x) {
        x.isEaten(10);
        System.out.println(x);
    }

    public void isEaten(final int hitPointsToDeduct) {
        this.hitPoints = this.hitPoints - hitPointsToDeduct;
    }
}

The advantage of this is that the hitPoints field is held centrally to one object. The Lion is not pulling out the value of the Dogs hitPoints. Look at this page for an explanation of the "Tell Dont Ask" concept.

EDIT

Having just had a play, I noticed that you're not setting the hitPoints value in either of your constructors and that your objects print out with the object reference rather than the details. For this, override the toString method. Here's the rewritten bits of the Dog clas:

public Dog(final int hitPoints, final String name, final int heightCMeters, final int lengthCMeters, final float weightKilos) {
    this.name = name;
    this.heightCMeters = heightCMeters;
    this.lengthCMeters = lengthCMeters;
    this.weightKilos = weightKilos;
    this.hitPoints = hitPoints;
}

@Override
public String toString() {
    final StringBuilder builder = new StringBuilder();
    builder.append("Name: ");
    builder.append(this.name);
    builder.append(", Height CM: ");
    builder.append(this.heightCMeters);
    builder.append(", Length CM: " );
    builder.append(this.lengthCMeters);
    builder.append(", Weight Kilos: ");
    builder.append(this.weightKilos);
    builder.append(", Hit Points: ");
    builder.append(this.hitPoints);
    return builder.toString();
}

So then with this main method:

public static void main(final String[] args) {
    final Lion adam = new Lion(500, "Adam", 5, 5, 5);
    final Dog fido = new Dog(500, "Fido", 5, 5, 5);
    adam.eat(fido);
}

I got the following output:

Name: Fido, Height CM: 5, Length CM: 5, Weight Kilos: 5.0, Hit Points: 490

Notice the hit points have been reduced from 500 to 490.

Based on the response of sleiman jneidi: You should create an abstract containing the hitPoints and the eat method (that it's the same behavior for each animal) Then you have not to wrote the method each time

abstract class X {
 public int hitPoints; 
 public void eat(X x) { // pass an edible object
  int hitPoints = x.hitPoints  - 10;
  System.out.println(x)
  }
}
// Lions are edible
class Lion extends X{


}  

//Dogs are edible as well
class Dog extends X{

}

The instance variable contained in the Animal class is inherited by the Lion and Dog class. It retains the value each time the eat(Aniamal a) method is called with an Animal object passed as a parameter. So than working on the instance variable contained in the Animal object that has been passed to the eat method we can perform various functions on the instance variable.

public class Animal {
    public int hitPoints;
}

public class Lion extends Animal {

    public String name;

    public Lion(String name) {
        this.name = name;
    }
    public void eat(Animal a) {
        a.hitPoints = a.hitPoints - 10;
        System.out.println(this.name + " Has: " + a.hitPoints + " HitPoints");
}

}


public class Dog extends Animal {

    public String name;

    public Dog(String name) {
        this.name = name;
    }
    public void eat(Animal a) {
        a.hitPoints = a.hitPoints - 10;
        System.out.println(this.name + " Has: " + a.hitPoints + " HitPoints");
}

}

public static void main(String[] args) {
    Cat adam = new Cat("adam");
    Lion dam = new Lion("dam");
    dam.eat(adam);
}       

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