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

时间:2013-12-18 10:09:14

标签: java

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

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

}
}

6 个答案:

答案 0 :(得分:1)

基本上,狮子会吃狗,反之亦然(这很奇怪,狗不够勇敢攻击狮子会)。无论如何,你需要的是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 

答案 1 :(得分:0)

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

    }

 }

答案 2 :(得分:0)

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

对于 Lion 类的eat方法,

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

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

答案 3 :(得分:0)

我会做任何可以吃的东西实现接口Edible。然后该接口可以有一个方法isEaten,它可以扣除生命值。

这样的事情:

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

然后你的狮子和狗会实施这个,以便他们可以吃掉。

Dog课程将是:

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的价值。请查看this page以获取“Tell Dont Ask”概念的解释。

修改

刚刚进行了游戏,我注意到你没有在你的任何一个构造函数中设置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。

答案 4 :(得分:0)

根据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{

}

答案 5 :(得分:0)

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