如何将类实例传递到另一个类,以便可以修改它?

时间:2015-12-10 00:18:55

标签: java

player1

目前我找不到将Player类fightMonster的实例传递给player1方法的方法,因为它位于不同的类中。我想通过整个player1课程,因为它最终将包括玩家的库存,经验,以前的HP等。 我能想到这样做的唯一方法就是将int hp的每一行分开,例如通过main并返回player1.setHp(int hp)并使用Player。但这并不能解决更复杂的应用程序(从战斗,经验等中获得的库存项目)。

即使我可以创建Player currentPlayer = new Player();类的新实例,如

Main

然后以某种方式返回.686 .model flat .code _KasparovDecend PROC ; named _test because C automaticedxly prepends an underscode, it is needed to interoperate push ebp mov ebp,esp ; stack pointer to ebp sub esp, 8 ;8/4 = 2 local variables mov ebx, [ebp+8] ; address of first array element mov ecx, [ebp+12] ; number of elements in array xor edx, edx ;make this 0 xor eax, eax ;make this 0, too push edi ;save this push ebx ;save this mov edi, ebx ;make a copy of first element in array add edi, 4 ;move 4 to find second element mov edx, [ebx] ;move first element into edx mov eax, [edi] ;move second element into eax mov [ebp - 4], ecx ;second ecx holder mov [ebp - 8], ecx ;this will be used to store eax while eax is used as a counter temporarily LoopTraverse: mov ebx, [ebp+8] ;reset for more than 1 loop mov edi, ebx ;make a copy of first element in array add edi, 4 ;move 4 to find second element mov edx, [ebx] ;move first element into edx mov eax, [edi] ;move second element into eax mov [ebp - 8], eax ;store eax into [ebp - 8] mov eax, [ebp+12] dec ecx ;number of elements decremented to 0 cmp ecx, 0 je AllDone LoopSwap: dec eax ;decrement the counter mov [ebp - 4], eax ;store decremented value into [ebp - 4] so it can be used next round cmp eax, 0 ;compares counter to number of elements -1 = number of comparisons to be made je LoopTraverse ;jump to LoopTraverse mov eax, [ebp - 8] ;return the value of eax cmp edx, eax ;comparing the two values jge NextElements ;if in order, just find next elements, else swap and find next elements mov eax, [edi] ;SWAPS THE TWO ELEMENTS mov edx, [ebx] mov [edi], edx mov [ebx], eax NextElements: add edi, 4 ;finds next mov eax, [edi] add ebx, 4 ;finds second element --------mov edx, [ebx] ;THIS IS THE REGISTER THAT WON'T MOVE TO THE NEXT ELEMENT4 ; IF A SWAP HAPPENS mov [ebp-8], eax ;stores value into ebp-8 mov eax, [ebp - 4] ;returns counter into eax jmp LoopSwap allDone: xor eax, eax pop ebx pop edi mov esp, ebp pop ebp ret _KasparovDecend ENDP END 将所有值匹配在一起......

这是我第一次发帖,请帮忙!

3 个答案:

答案 0 :(得分:1)

假设你想要执行一个有一个参数的方法。 你可以这样:

 String variable = "Hello";
 doSomething(variable);

 public void doSomething(String variable){}

您调用的方法的参数必须与调用方法时传递的变量类型相同。

所以在你的情况下我会建议:

 public class Main {
      Player player1 = new Player(); //You create a new Player
      FightMonster actions = new FightMonster();

      player1 = actions.getHit(player1); //You call the method "getHit" and pass the values of player1
 }

 public class FightMonster {
      //The value of "player" its the same value of "player1" that you pass when you called this method
      public Player getHit(Player player) {
           player.setHp(player.getHp - 10); //You change the hp of the player
           return player; //And return the new value of the player
      }
 }

返回值后,调用方法时传递的玩家将返回新值。

答案 1 :(得分:1)

public class Player{

    public static void main(String [] a){

    Player player1 = new Player();
    Player player2 = new Player();

    //You can either use constructor of FightMonster class to pass your object reference
    FightMonster object = new FightMonster(player1);

    //or you can setup another method in FightMonster which will receive the reference of the player class
    object.passReference(player1);
    }        
}
class FightMonster{

    Player player;
    public FightMonster(Player player){

          this.player = player;
          //Now, In this class you can use player reference
    }
    public void passReference(Player player){
          this.player = player;
    }
}

如果要传递FightMonster类引用的类Player - 将不会被任何其他类使用,那么在这种情况下,您也可以将其作为内部类。 例如这里我创建了一个方法本地内部类,它在main方法中定义。

public class Player{

    public static void main(String [] a){

    Player player1 = new Player();
    Player player2 = new Player();

    class FightMonsterInner{
        public void displayInformation(){
            //here, you can access variables and instance defined in outer class.
        }
    }

    FightMonsterInner innerObject = new FightMonsterInner();
    innerObject.displayInformation();

    }   
}

答案 2 :(得分:0)

is there a specific reason for fightMonster method to be in another class? I would do something like...

   public class Player {
     private int hp = 100;
     //with appropriate getters and setters, constructors etc.
     public void fightMonster() {
          this.setHp(this.hp - 10);
     }
    }

... then you just need to call p1.fightMonster(); to get the proper result. Hope it helps!