[英]Understanding inheritance in practice. Printing values of an instance
I have a class of Agents with a working __str__
method.我有一类具有工作
__str__
方法的代理。 I have a class of Family(Agents)
.我有一类
Family(Agents)
。 The Family
is structured as a dictionary with the Agent ID as key. Family
结构为字典,以代理 ID 为键。 After allocating agents to families, I iterate over my_families
: When I print members.keys()
, I get the correct keys.将代理分配给家庭后,我遍历
my_families
:当我打印members.keys()
,我得到了正确的密钥。 When I print members.values()
, I get a list of instances当我打印
members.values()
,我得到一个实例列表
But, I cannot access the values themselves inside the instances.但是,我无法访问实例中的值本身。 When I try to use a method
get_money()
I get an answer that Family class does not have that method.当我尝试使用
get_money()
方法时,我得到的答案是 Family 类没有该方法。 Any help?有什么帮助吗?
for family in my_families:
print family.members.values()
Thanks谢谢
Providing more information.提供更多信息。 Family class
家庭班
class Family(agents.Agent):
"""
Class for a set of agents
"""
def __init__(self, family_ID):
self.family_ID = family_ID
# _members stores agent set members in a dictionary keyed by ID
self.members = {}
def add_agent(self, agent):
"Adds a new agent to the set."
self.members[agent.get_ID()] = agent
Class of agents代理类别
class Agent():
"Class for agent objects."
def __init__(self, ID, qualification, money):
# self._ID is unique ID number used to track each person agent.
self.ID = ID
self.qualification = qualification
self.money = money
def get_ID(self):
return self.ID
def get_qual(self):
return self.qualification
def get_money(self):
return self.money
def __str__(self):
return 'Agent Id: %d, Agent Balance: %d.2, Years of Study: %d ' (self.ID, self.money, self.qualification)
#Allocating agents to families
def allocate_to_family(agents, families):
dummy_agent_index = len(agents)
for family in families:
dummy_family_size = random.randrange(1, 5)
store = dummy_family_size
while dummy_family_size > 0 and dummy_agent_index >= 0 and (dummy_agent_index - dummy_family_size) > 0:
family.add_agent(agents[dummy_agent_index - dummy_family_size])
dummy_family_size -= 1
dummy_agent_index -= store
Finally the printing example that gets me instance objects but not their values最后是打印示例,它获取我的实例对象而不是它们的值
for family in my_families:
print family.members.values()
If Family
is supposed to contain Agent
objects, why does it inherit from Agent
?如果
Family
应该包含Agent
对象,为什么它继承自Agent
? Regardless, you never initialized the parent Agent
object in the Family
class's __init__
, and according to your edit the get_money
method is contained in the Agent
class, so Family
objects don't have a get_money
method.无论如何,您从未在
Family
类的__init__
初始化父Agent
对象,并且根据您的编辑, get_money
方法包含在Agent
类中,因此Family
对象没有get_money
方法。 To access that, you would need to first access a Family
object's members
dictionary, then use a key to access the desired Agent
object, then access that object's get_money
method.要访问它,您需要首先访问
Family
对象的members
字典,然后使用密钥访问所需的Agent
对象,然后访问该对象的get_money
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.