2013-12-18 189 views
1

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

我想让狮子吃狮子类中存储在一个新变量中的实例变量的Dog和减号。

类狮子

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) 

} 
} 
+2

好奇你为什么不在这里使用继承,而不是它回答你的问题,但它是一个很好的机会。 – ChiefTwoPencils

+0

您能否详细说明一下? – Aditya

+0

例如,如果狮子吃了狗,那么狗有1000点生命值,而不是sooem生命值必须从狗减去,例如 Lion adam = new Lion(); 狗食=新狗(); adam.eat(chow); –

回答

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 
+0

@GarciaJulien但是,也许不是每个猎物都是捕食者,理想的方式是定义两个接口Predator(包含方法eat)和Prey,所以Lion类实现了两个接口 –

0

最佳方式写测试类或写狮级,这将维持两个类的生命值主要方法。

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

    } 

} 
+0

因此,当oldLion.eat(小狗)吃什么()方法? –

+0

为了减少生命值....他必须使用继承的最佳方式。 –

0

你必须有一个抽象类Animal,定义在此处所有的常用方法。

对于狮子类的eat方法,

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

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

+0

并将[animalName]细节更改为'details()'。 – ChiefTwoPencils

0

我会做任何可以吃的实现一个接口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的价值。请看this page,了解“Tell Dont Ask”概念的解释。

编辑

刚走了一出戏,我注意到,你没有在任何你的构造函数的设定值,生命值和你的对象打印出与对象的引用,而不是细节。为此,重写toString方法。这里的狗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.

0

根据sleiman jneidi的回复: 您要创建一个包含生命值和食用方法的抽象(它的每个动物相同的行为),那么你有没有每次写的方法

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{ 

} 
0

包含在动物类中的实例变量是由遗传狮子和狗类。 每次使用作为参数传递的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); 
}  
相关问题