Suppose I have this cat class and I make 2 instance of it. I want cat can attack each other
假设我有一个cat类,我做了两个实例。我想让猫互相攻击
class ninjaCat {
var health : Double = 100.00
var attack = Double()
init(attack : Double){
self.attack = attack
}
func thunderClaw(otherCat : ninjaCat){
health = otherCat.health
otherCat.health = health - self.attack
}
}
var NinjaCat1 = ninjaCat(10.60)
var NinjaCat2 = ninjaCat(20.15)
NinjaCat1.thunderClaw(NinjaCat2)
Is it posible to pass class object as function parameter?
是否可以将类对象作为函数参数传递?
1 个解决方案
#1
1
It is possible indeed. However you should check you code there because your using the attacking cat's current life instead of the target's to compute the remaining life:
这确实是可能的。但是,您应该检查您的代码,因为您使用攻击猫的当前生命而不是目标的生命来计算剩余生命:
func thunderClaw(otherCat : ninjaCat){
otherCat.health = health - self.attack
}
should be
应该是
func thunderClaw(otherCat : ninjaCat){
otherCat.health = otherCat.health - self.attack
}
or simply
或者简单地
func thunderClaw(otherCat : ninjaCat){
otherCat.health -= self.attack
}
#1
1
It is possible indeed. However you should check you code there because your using the attacking cat's current life instead of the target's to compute the remaining life:
这确实是可能的。但是,您应该检查您的代码,因为您使用攻击猫的当前生命而不是目标的生命来计算剩余生命:
func thunderClaw(otherCat : ninjaCat){
otherCat.health = health - self.attack
}
should be
应该是
func thunderClaw(otherCat : ninjaCat){
otherCat.health = otherCat.health - self.attack
}
or simply
或者简单地
func thunderClaw(otherCat : ninjaCat){
otherCat.health -= self.attack
}