0
A method that influences two classes in python
for example a metjod attack that subtracts the attack from the health. It's possible?if yes, how?
4 odpowiedzi
+ 6
Pass the Instance of one class as a parameter by reference into the function of the other class.
0
You could just have a method that takes as arguments the health to substract and the object to take it from, and a method in the other object to substract health :
class fighter():
def attack(self, other, health):
other.substract(health)
def substract(self, health):
self.health -= health
obj1 = fighter
obj2 = fighter
obj1.attack(obj2, 10)
*edit: forgot to add self reference
0
@spcan yes, but two different classes? you just made one class covering all fighters, and if I want different type of fighters?
0
@Leonardo Brocchi there are several options
1. you copy the method to each figther class, which gives you the ability to alter them at any time given
2. you create a general class and inherit from it
3. you could use just a general class that, by expanding it, allows you to have every aspect of it customizable
If what you are worried about is an object calling a method of a different class object don't be.