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?

18th Apr 2017, 10:32 AM
Leonardo Brocchi
4 Antworten
+ 6
Pass the Instance of one class as a parameter by reference into the function of the other class.
18th Apr 2017, 10:40 AM
Daniel Thomalla
Daniel Thomalla - avatar
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
18th Apr 2017, 11:21 AM
spcan
spcan - avatar
0
@spcan yes, but two different classes? you just made one class covering all fighters, and if I want different type of fighters?
18th Apr 2017, 2:17 PM
Leonardo Brocchi
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.
18th Apr 2017, 3:18 PM
spcan
spcan - avatar