简体   繁体   English

使用另一个类中的方法从一个类中获取实例变量?

[英]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)? 我如何从Dog类中获取实例变量生命值,并通过eat(X x)方法将其传递给Lion类?

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. 我试图让Lion吃掉实例变量中的Dog和减点,该实例变量存储在Lion类的新变量中。

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 ,该类应包含您提到的hitPoint

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. 最好的方法是为Lion类编写一个测试类或编写main方法,这将维护两个类的生命值。

 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. 您必须具有一个抽象类Animal ,在其中定义了所有常用方法。

For eat method of Lion class, 对于狮子课的eat

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

For eat method of Dog class, also the same logic. 对于Dog类的eat方法,逻辑也相同。

I would make anything that can be eaten implement an interface Edible . 我将通过Edible接口使任何可以吃的东西。 Then that interface can have a method isEaten that takes the hit point deduction. 然后,该接口可以具有isEaten方法,该方法可以计算出生命值。

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. 这样做的好处是, hitPoints字段集中在一个对象的中央。 The Lion is not pulling out the value of the Dogs hitPoints. 狮子并没有拉出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. 刚玩完游戏,我注意到您没有在任何一个构造函数中设置hitPoints值,并且您的对象使用对象引用而不是细节进行打印。 For this, override the toString method. 为此,重写toString方法。 Here's the rewritten bits of the Dog clas: 这是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. 请注意,生命值从500降低至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 基于sleiman jneidi的响应:您应该创建一个包含hitPoints和eat方法的摘要(每个动物的行为相同),然后不必每次都编写该方法

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. Animal类中包含的实例变量是Lion和Dog类继承的。 It retains the value each time the eat(Aniamal a) method is called with an Animal object passed as a parameter. 每次通过将Animal对象作为参数调用eat(Aniamal a)方法时,它都会保留该值。 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. 因此,与处理传递给eat方法的Animal对象中包含的实例变量相比,我们可以对实例变量执行各种功能。

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);
}       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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