如何在方法参数中调用一个对象?

huangapple go评论62阅读模式
英文:

How do I call an object as method parameter?

问题

好的,以下是您要翻译的内容:

"我想制作一个游戏。我引入了一些角色(作为一个类),带有生命值和伤害参数。现在我想创建一个名为“PerformHit”的方法,用于执行一些攻击。就像这样:

class 战斗 {
    int 执行攻击(对象1, 对象2) {
        return 对象1.生命值 - 对象2.伤害;
    }
}

然而,我不能将对象作为方法参数进行调用。我该如何处理?"

英文:

Say, I wanna make a game. I introduced some characters (as a class), with health and damage parameters. Now I wanna create a method called "PerformHit" that would perform some attack. Like

class fight {
int PerformHit(object1,object2){

return object1.health - object2.damage;
 }
}

However, I cannot call an object as method parameter. How do I deal with that?

答案1

得分: 0

java是完全类型化的。因此,请为这些参数指定类型。`int performHit(Player player, Weapon weapon) { return player.health - weapon.damage; }`。
英文:

java is fully typed. So type those parameters. int performHit(Player player, Weapon weapon) { return player.health - weapon.damage; }.

答案2

得分: -1

class fight {
int PerformHit(Object object1, Object object2){
return object1.health - object2.damage;
}
}

英文:
class fight {
int PerformHit(Object object1, Object object2){

return object1.health - object2.damage;
 }
}

huangapple
  • 本文由 发表于 2020年9月1日 06:09:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63678816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定