英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论