我正在学习python中的对象和类,我试图编写一个程序,要求我在不同的阶梯上打印动物的特征

2024-04-20 07:31:53 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个梯子。梯子的第三级有一只猴子,第五级有一只松鼠,第八级有一只鸽子,第十五级有一只鹰,第十七级有另一只猴子。你知道吗

猴子有两只眼睛,两只手,两条腿。这只松鼠有两只眼睛,四条腿。鸽子有两只眼睛,两只翅膀,两条腿,会飞。鹰有两只眼睛,两只翅膀,两条腿,能飞。你知道吗

我将代码分为两个模块梯形图用户和梯形图用户

梯子-用户.py 打印动物特征

 from ladderutils import ladder 

    l = ladder()
    print (l.animal_at_rung(3))
    print (l.animal_at_rung(5))
    print (l.animal_at_rung(8))
    print (l.animal_at_rung(15))
    print (l.animal_at_rung(10))
    print (l.get_animals_count())
    print (l.animal_at_rung(3) == l.animal_at_rung(17))
    print (type(l.animal_at_rung(3)) == type(l.animal_at_rung(17)))
    print (l.animal_at_rung(8).fly())
    print (l.animal_at_rung(3).fly())
    print (l.hop(3))
    print (l.animal_at_rung(3))
    print (l.animal_at_rung(4))
    print (l.hop(4))
    print (l.animal_at_rung(4))

你知道吗梯形图.py你知道吗

class Animal(object):
  features= {}

  def __init__(self,eyes,legs,hands,wings,fly):
    self.features['eyes'] = eyes
    self.features['legs'] = legs
    self.features['hands'] = hands
    self.features['wings'] = wings
    self.features['fly'] = fly

  def fly(self):
    return self.features['fly']

class Monkey(Animal):

  def __init__(self,eyes,legs,hands):
    self.name = 'Monkey'
    Animal.__init__(self,eyes,legs,hands,0,False)

  def __repr__(self):
    temp = {}
    for key in self.features:
        if(self.features[key]>0 and self.features[key]!=False):
            temp[key] = self.features[key]
    return str({self.name:temp})

class Squirrel(Animal):

  def __init__(self,eyes,legs):
    self.name = 'Squirrel'
    Animal.__init__(self,eyes,legs,0,0,False)        

  def __repr__(self):
    temp = {}
    for key in self.features:
        if(self.features[key]>0 or self.features[key]!=False):
            temp[key] = self.features[key]
    return str({self.name:temp})

class Pigeon(Animal):

  def __init__(self,eyes,legs,wings):
    self.name = 'Pigeon'
    Animal.__init__(self,eyes,legs,0,wings,True)

  def __repr__(self):
    print(self.features)
    temp = {}
    for key in self.features:
        if(self.features[key]>0 or self.features[key]==True):
            temp[key] = self.features[key]
    return str({self.name:temp})

class Eagle(Animal):

  def __init__(self,eyes,legs,wings):
    self.name = 'Eagle'
    Animal.__init__(self,eyes,legs,0,wings,True)

  def __repr__(self):
    temp = {}
    for key in self.features:
        if(self.features[key]!=0 and self.features[key]!=False):
            temp[key] = self.features[key]
    return str({self.name:temp})

class ladder:
  ladder_pos = {}


  def __init__(self):
    self.ladder_pos['3'] = Monkey(2,2,2)
    self.ladder_pos['5'] = Squirrel(2,4)
    self.ladder_pos['8'] = Pigeon(2,2,2)
    self.ladder_pos['15'] = Eagle(2,2,2)
    self.ladder_pos['17'] = Monkey(2,2,2)

  def animal_at_rung(self,pos):
    if(str(pos) in self.ladder_pos):
      return self.ladder_pos[str(pos)]
    else:
      return ('None')

  def get_animals_count(self):
    return len(self.ladder_pos)

  def hop(self,pos):
    if(str(pos+1) in self.ladder_pos):
      return ("Not Empty")
    else:
      self.ladder_pos[str(pos+1)] = self.ladder_pos[str(pos)]
      del self.ladder_pos[str(pos)]
      return ('None')

我的输出是

{'Monkey': {'legs': 2, 'wings': 2, 'fly': True, 'eyes': 2}}
{'Squirrel': {'legs': 2, 'wings': 2, 'fly': True, 'eyes': 2}}
{'hands': 0, 'legs': 2, 'fly': True, 'wings': 2, 'eyes': 2}
{'Piegon': {'legs': 2, 'wings': 2, 'fly': True, 'eyes': 2}}
{'Eagle': {'legs': 2, 'wings': 2, 'fly': True, 'eyes': 2}}
None
4
False
False
True
True
None
None
{'Monkey': {'legs': 2, 'wings': 2, 'fly': True, 'eyes': 2}}
Not Empty
{'Monkey': {'legs': 2, 'wings': 2, 'fly': True, 'eyes': 2}}

但我应该得到

Monkey <eyes: 2, legs: 2, hands: 2>
Squirrel <eyes: 2, legs: 4>
Pigeon <fly: True, eyes: 2, legs: 2, wings: 2>
Eagle <fly: True, eyes: 2, legs: 2, wings: 2>
None
5
False
True
True
False
None
None
Monkey <eyes: 2, legs: 2, hands: 2>
Not empty
Monkey <eyes: 2, legs: 2, hands: 2>

Tags: keyposselftruedefatfeaturesprint
1条回答
网友
1楼 · 发布于 2024-04-20 07:31:53

此处:

class Animal(object):
  features= {}

  def __init__(self,eyes,legs,hands,wings,fly):
    self.features['eyes'] = eyes
    self.features['legs'] = legs
    self.features['hands'] = hands
    self.features['wings'] = wings
    self.features['fly'] = fly

features是在Animal的所有实例之间共享的类属性。因此,每次实例化Animal(或的子类)时,都会覆盖上一个实例设置的值。您想要的是将features改为实例属性:

class Animal(object):

  def __init__(self,eyes,legs,hands,wings,fly):
    self.features = {
        'eyes': eyes,
        'legs': legs,
        'hands': hands,
        'wings': wings,
        'fly': fly
        }

另外,您可以在每个子类和任何子类中复制粘贴__repr__方法,而您只需在Animal中定义它,实际上您可以简化它:

class Animal(object):
  def __init__(self,eyes,legs,hands,wings,fly):
    self.features = {
        'eyes': eyes,
        'legs': legs,
        'hands': hands,
        'wings': wings,
        'fly': fly
        }

    def __repr__(self):
        temp = ", ".join(
            "{}: {}".format(k, v) 
            for k, v in self.features.items() if v
            )
        return "{} <{}>".format(self.name, temp)

还有一些其他问题-ladder类中的另一个类属性(根据Python命名约定应该命名为Ladder),在ladder.ladder_pos中使用strings键,然后在作用于它的方法中使用integers参数并一次又一次地将它们转换为字符串(提示:您可以只使用integers作为键)等等。。。,但是这些不应该阻止您的代码产生预期的输出。你知道吗

相关问题 更多 >