繁体   English   中英

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

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

我如何从Dog类中获取实例变量生命值,并通过eat(X x)方法将其传递给Lion类?

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

类狗

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)

}
}

基本上,狮子可以吃狗,反之亦然(这很奇怪,狗没有勇气攻击狮子)。 无论如何,您需要的是一个代表吃动物的动物的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)
  }
}

现在,狮子要吃狗

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

最好的方法是为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);

    }

 }

您必须具有一个抽象类Animal ,在其中定义了所有常用方法。

对于狮子课的eat

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

对于Dog类的eat方法,逻辑也相同。

我将通过Edible接口使任何可以吃的东西。 然后,该接口可以具有isEaten方法,该方法可以计算出生命值。

像这样:

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

然后,您的狮子和狗会执行此操作,以便可以食用它们。

狗类将是:

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

和狮子班:

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

这样做的好处是, hitPoints字段集中在一个对象的中央。 狮子并没有拉出Dogs hitPoints的价值。 请在此页面上获得有关“不要问”概念的解释。

编辑

刚玩完游戏,我注意到您没有在任何一个构造函数中设置hitPoints值,并且您的对象使用对象引用而不是细节进行打印。 为此,重写toString方法。 这是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();
}

因此,使用这种主要方法:

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

我得到以下输出:

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

请注意,生命值从500降低至490。

基于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{

}

Animal类中包含的实例变量是Lion和Dog类继承的。 每次通过将Animal对象作为参数调用eat(Aniamal a)方法时,它都会保留该值。 因此,与处理传递给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