对象属性的更改在主类中不是永久性的?

时间:2017-05-28 15:56:10

标签: java class oop object

所以,在我的主要课程中,我试图从另一个类调用此方法。

public class Heroes {

private String name;
private int hp;
private int mana;
private int armor;
private int attack;
private int strength;
private int agility;
private int intelligence;

public Heroes(String name, int hp, int mana, int armor, int attack, int strength, int agility, int intelligence) {
    this.name = name;
    this.hp = hp;
    this.mana = mana;
    this.armor = armor;
    this.attack = attack;
    this.strength = strength;
    this.agility = agility;
    this.intelligence = intelligence;
}

public String getName() {
    return name;
}

public int getHp() {
    return hp;
}

public int getMana() {
    return mana;
}

public int getArmor() {
    return armor;
}

public int getAttack() {
    return attack;
}

public int getStrength() {
    return strength;
}

public int getAgility() {
    return agility;
}

public int getIntelligence() {
    return intelligence;
}

但我意识到对象的HP在受到攻击时不会永久性地减少。如果我在C编码,我将能够使用指针来解决这个问题但在java中我没有解决方案。上述方法与对象的规范属于同一类。

谢谢!

编辑:我所谈论的课程是:

public class Juggernaut extends Heroes {

private boolean skill1;
private boolean skill2;
private boolean skill3;
private boolean skill4;

public Juggernaut(int hp, int mana, int armor, int attack, int strength, int agility, int intelligence, boolean skill1, boolean skill2, boolean skill3, boolean skill4) {
    super("Juggernaut", hp, mana, armor, attack, strength, agility, intelligence);
    this.skill1 = skill1;
    this.skill2 = skill2;
    this.skill3 = skill3;
    this.skill4 = skill4;
}

double currentHp(){
    return getHp() + (10 * getArmor()) + (2.5 * getStrength());
}
double currentMana(){
    return getMana() + (2.5 * getIntelligence());
}
double currentDmg(){
    return getAttack() + (1.0 * getAgility());
}

void Attack(String attackerName, String defenderName, double attackerDmg, double defenderHp){
    System.out.println(attackerName + " attacked " + defenderName + " causing " + attackerDmg +  " damage.");
    defenderHp -= attackerDmg;
    System.out.println(defenderName + " now has " + defenderHp + " HP.");
}


public boolean getSkill1() {
    return skill1;
}

public boolean getSkill2() {
    return skill2;
}

public boolean getSkill3() {
    return skill3;
}

public boolean getSkill4() {
    return skill4;
}

}

public class Pudge extends Heroes {

private boolean skill1;
private boolean skill2;
private boolean skill3;
private boolean skill4;

public Pudge(int hp, int mana, int armor, int attack, int strength, int agility, int intelligence, boolean skill1, boolean skill2, boolean skill3, boolean skill4) {
    super("Pudge", hp, mana, armor, attack, strength, agility, intelligence);
    this.skill1 = skill1;
    this.skill2 = skill2;
    this.skill3 = skill3;
    this.skill4 = skill4;
}

double currentHp(){
    return getHp() + (10 * getArmor()) + (2.5 * getStrength());
}
double currentMana(){
    return getMana() + (2.5 * getIntelligence());
}
double currentDmg() {
    return getAttack() + (1.0 * getAgility());
}

void Attack(String attackerName, String defenderName, double attackerDmg, double defenderHp){
    System.out.println(attackerName + " attacked " + defenderName + " causing " + attackerDmg +  " damage.");
    defenderHp -= attackerDmg;
    System.out.println(defenderName + " now has " + defenderHp + " HP.");


}





public boolean getSkill1() {
    return skill1;
}

public boolean getSkill2() {
    return skill2;
}

public boolean getSkill3() {
    return skill3;
}

public boolean getSkill4() {
    return skill4;
}

}

public class Main {
public static void main(String[] args){

    Juggernaut juggernaut = new Juggernaut(200,75,0,26,20,26,14,true,true,true,true);
    Pudge pudge = new Pudge(200,75, -1,33,25,14,14,true,true,true,true);

    juggernaut.Attack(juggernaut.getName(),pudge.getName(),juggernaut.currentDmg(), pudge.currentHp());
    pudge.Attack(pudge.getName(), juggernaut.getName(), pudge.currentDmg(),juggernaut.currentHp());



}

}

<template is="dom-repeat" repeat="{{myItems}}">
  One:   <paper-input type="number" value="{{myValue1}}"></paper-input>
  Two:   <paper-input type="number" value="{{myValue2}}"></paper-input>
  Three: <paper-input type="number" value="{{myValue3}}"></paper-input>
  <template is="dom-if" if="[[_show(myValue1)]]">
      Four: <paper-input value="{{myValue4}}></paper-input>
  </template>
</tempalte>

}

3 个答案:

答案 0 :(得分:2)

在java而不是传递double时,您会传递包含double的对象,以便您可以修改double

更好的是,您将以不同的方式构造代码,以便double和对其进行操作的函数都属于同一个类,因此您不必将double传递给函数的参数;相反,该函数将通过double修改this

修改

class MyObjectWhichCanBeAttacked
{
    private double healthPoints;

    void applyAttack( double healthPointsToSubtract )
    {
        this.healthPoints -= healthPointsToSubtract;
    }
}

答案 1 :(得分:1)

您遇到的问题是您正在减少要传递的变量defenderHP的值(参数),而不是实际对象的值。当方法完成执行时,defenderHP超出范围并被销毁。这个问题的一个可能的解决方案是为类中的变量设置get和set方法。如果你有:

//In your Hero class
public void reduceHp(int damage)
{
    this.hp= hp - damage;
}

现在,您通过传递两个对象而不是每个所需的值来修改Attack方法:

public void Attack(Hero attacker, Hero defender)
{
    //decrease the defender's HP by the attacker's attack
    defender.reduceHP(attacker.getAttack());

    //Print out names and damaged caused
    System.out.println(attacker.getName() + " has attacked " + defender.getName() + " causing " + attacker.getAttack());


    System.out.println(defender.getName() + " now has " + defender.getHP());
}

答案 2 :(得分:1)

您必须了解变量范围。有三种类型的变量,

  

本地变量:

     
      
  • 在方法/构造函数或块中声明方法时。
  •   
  • 仅在区块内有效。
  •   
  • 在街区内创建并销毁。
  •   
     

实例/成员变量:

     
      
  • 在类中声明,但在方法之外。
  •   
  • 也称为成员或字段变量。
  •   
  • 在对象被销毁时创建和销毁对象时创建。
  •   
  • 在定义类的所有方法和构造函数中都可见。
  •   
     

类/静态变量:

     
      
  • 在类中使用static关键字声明。
  •   
  • 只有一个副本用于类,无论从中创建了多少个对象。
  •   
  • 存储在静态存储器中。
  •   
     

最后要记住的一件事:

     

变量的可变持续时间或生命周期:它在销毁之前在内存中存在的时间。

要回答您的问题:在此Attack()中您有本地变量。将其设为实例/成员变量

void Attack(String attackerName, String defenderName, double attackerDmg, double defenderHp){
    System.out.println(attackerName + " attacked " + defenderName + " causing " + attackerDmg +  " damage.");
    defenderHp -= attackerDmg;
    System.out.println(defenderName + " now has " + defenderHp + " HP.");
}

正如我之前提到的:double defenderHp是一个参数。它将被销毁的范围结束。为了避免这种情况,你可以在方法(实例变量)之外声明一个变量。

作为:double defenderHp;

Attack方法内:this.defenderHp -= attackerDmg;

您可以实施getter和setter。但是这个变量的滚动是为了更新价值。因此,由于这个原因,这是不方便的(这些类型的变量称为mutators)。 learn more about Accessors and Mutatorsthis question