英文:
Python Parent class data access inheritance
问题
class Player:
def __init__(self, thickness):
from PlayerAnimator import PlayerAnimator
self.thickness = thickness
self.isAlive = True
self.animator = PlayerAnimator(5)
def death(self):
self.isAlive = False
self.animator.animateDeath(2)
from Player import Player
class PlayerAnimator(Player):
def __init__(self, animationSpeed):
self.globalAnimationSpeed = animationSpeed
def animate(self, deathSpeed):
print(self.thickness)
英文:
Hi I have a problem with accessing data through a child class.
I have a Player class defined like so in Player.py:
class Player:
def __init__(self, thickness):
from PlayerAnimator import PlayerAnimator
self.thickness = thickness
self.isAlive = True
self.animator = PlayerAnimator(5)
def death(self):
self.isAlive = False
self.animator.animateDeath(2)
Now PlayerAnimator is defined in separate file called PlayerAnimator.py:
And the reason I'm having it as a child is as following: I want every player in the game to have it's separate animator and that animator needs to be able to access player's data like position, thickness, etc in it's methods
from Player import Player
class PlayerAnimator(Player):
def __init__(self, animationSpeed):
self.globalAnimationSpeed = animationSpeed
def animate(self, deathSpeed):
print(self.thickness)
Now when I try to print self.thickness by calling death() inside of some Player's method It says that it doesn't exist. I also tried super().thickness and it also doesn't work.
The point is to have Player instantiated and then as Player is instantiated an animator is being instantied, assigned to Player and it has access to Player's data.
How do I fix this?
答案1
得分: 3
I want every player in the game to have its separate animator and that animator needs to be able to access player's data like position, thickness, etc in its methods.
This is a case for composition (has a
relationship), not inheritance (is a
relationship). And it looks like you understand it: have Player instantiated and then as Player is instantiated, an animator is being instantiated, assigned to Player
animator.py:
class Animator():
def __init__(self, player, speed):
self.player = player
self.global_speed = speed
def animate(self, speed):
print(f'Player thickness {self.player.thickness}')
Note, it's not clear what Animator.global_speed and speed
argument of animate()
would do, but anyway...
player.py:
from animator import Animator
class Player:
def __init__(self, thickness):
self.thickness = thickness
self.isAlive = True
self.animator = Animator(self, 5)
def death(self):
self.isAlive = False
self.animator.animate(2)
player = Player(10)
player.death()
output:
Player thickness 10
Given more information, it may be possible to have a better approach here or there. And the more I think about it, I have doubts whether your Player + Animator setup is the best approach.
英文:
I want every player in the game to have it's separate animator and that animator needs to be able to access player's data like position, thickness, etc in it's methods.
This is case for composition (has a
relationship), not inheritance (is a
relationship). And it looks like you understand it: have Player instantiated and then as Player is instantiated an animator is being instantied, assigned to Player
animator.py:
class Animator():
def __init__(self, player, speed):
self.player = player
self.global_speed = speed
def animate(self, speed):
print(f'Player thickness {self.player.thickness}')
Note, it's not clear what Animator.global_speed and speed
argument of animate()
would do, but anyway...
player.py:
from animator import Animator
class Player:
def __init__(self, thickness):
self.thickness = thickness
self.isAlive = True
self.animator = Animator(self, 5)
def death(self):
self.isAlive = False
self.animator.animate(2)
player = Player(10)
player.death()
output:
Player thickness 10
Given more information it may be possible to have a better approach here or there. And the more I think about it, I hove doubts whether your Player + Animator setup is the best approach.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论