Python練習題答案: PatternCraft - 戰略【難度:2級】--景越Python編程實例訓練營,1000道上機題等你來挑戰

PatternCraft - 戰略【難度:2級】:

答案1:

class Move(object):
    def move(self, unit): unit.position += self.unit

class Fly(Move):  unit = 10
class Walk(Move): unit = 1
    
class Viking(object):
    def __init__(self):
        self.move_behavior = Walk()
        self.position = 0
  
    def move(self): self.move_behavior.move(self)

答案2:

class Fly:
    @staticmethod
    def move(unit):
        unit.position += 10


class Walk:
    @staticmethod
    def move(unit):
        unit.position += 1


class Viking:
    def __init__(self):
        self.move_behavior = Walk()
        self.position = 0

    def move(self):
        self.move_behavior.move(self)

答案3:

class Fly():
    @staticmethod
    def move(unit):
        #your code here 
        unit.position += 10
        
class Walk():
    @staticmethod
    def move(unit):
        #your code here
        unit.position += 1

class Viking():
    def __init__(self):
        #your code here
        self.move_behavior = Walk()
        self.position = 0
  
    def move(self):
        #your code here
        self.move_behavior.move(self)

答案4:

class Fly():
    def move(self,unit):
        unit.position = unit.position + 10
        
class Walk():
    def move(self,unit):
        unit.position = unit.position + 1

class Viking():
    def __init__(self):
        self.position = 0
        self.move_behavior = Walk()
  
    def move(self):
        self.move_behavior.move(self)

答案5:

class Fly():
    def move(self,unit):
        unit.position+=10
        
class Walk():
    def move(self,unit):
        unit.position+=1

class Viking():
    def __init__(self):
        self.position=0
        self.move_behavior = Walk()
  
    def move(self):
        self.move_behavior.move(self)

答案6:

class Fly():
    def move(self,unit):
        #your code here
        unit.position += 10
        
class Walk():
    def move(self,unit):
        #your code here
        unit.position += 1

class Viking():
    def __init__(self):
        #your code here
        self.position = 0
        self.move_behavior = Walk()
  
    def move(self):
        #your code here
        self.move_behavior.move(self)

答案7:

class Fly():
  def move(self,unit):
    unit.position += 10
        
class Walk():
   def move(self,unit):
    unit.position += 1
        #your code here

class Viking():
  def __init__(self):
    self.position = 0
    self.move_behavior = Walk()

  def move(self):
    self.move_behavior.move(self)

答案8:

class Fly:
    def move(self, unit):
        unit.position += 10
        
class Walk:
    def move(self, unit):
        unit.position += 1

class Viking:
    def __init__(self):
        self.position = 0
        self.move_behavior = Walk()
  
    def move(self):
        self.move_behavior.move(self)

答案9:

class Fly():
    def move(self,unit):
        unit.position += 10
        #your code here
        
class Walk():
    def move(self,unit):
        unit.position += 1
        #your code here

class Viking():
    def __init__(self):
        self.position = 0
        self.move_behavior = Walk()
        
        #your code here
  
    def move(self):
        self.move_behavior.move(self)
        #your code here​

答案10:

class Fly():
    def move(self,unit):
        unit.position+=10
        #your code here
        
class Walk():
    def move(self,unit):
        unit.position+=1
        #your code here

class Viking():
    def __init__(self):
        self.position=0
        self.move_behavior=Walk()
  
    def move(self):
        self.move_behavior.move(self)
        #your code here​

答案11:

class Move(object):
    def move(self,unit): return unit.position + self.UNIT
    
class Fly(Move):  UNIT = 10
class Walk(Move): UNIT = 1

class Viking(object):
    def __init__(self):
        self.move_behavior = Walk()
        self.position = 0
  
    def move(self): self.position = self.move_behavior.move(self)

答案12:

class Fly():
    def move(self): return 10
        
class Walk():
    def move(self): return 1

class Viking():
    def __init__(self): self.move_behavior, self.position = Walk(), 0
    def move(self): self.position += self.move_behavior.move()

答案13:

class Fly():
    def move(self):
        self.unit = 10 
        return self.unit
        
class Walk():
    def move(self):
        self.unit = 1
        return self.unit

class Viking():
  
    def __init__(self):
        self.position = 0
        self.move_behavior = Walk()
        
    def move(self):
      self.position = self.position + self.move_behavior.move()

答案14:

class Fly():
    def __init__(self, unit = 10):
        self.unit = unit      
        
class Walk():
    def __init__(self, unit=1):
        self.unit = unit
        
class Viking():
    def __init__(self, position = 0):
        self.position = position
        self.move_behavior = Walk()
  
    def move(self):
        self.position += self.move_behavior.unit
        return self.position​

答案15:

class Fly():
    def move(self,unit):
     unit.position+=10
        
class Walk():
    def move(self,unit):
      unit.position+=1

class Viking():
    def __init__(self):
      self.move_behavior=Walk()
      self.position=0
  
    def move(self):
      return self.move_behavior.move(self)

答案16:

class Fly():
    def move(self,unit):
        return unit*10
        
class Walk():
    def move(self,unit):
        return unit

class Viking():
    def __init__(self):
        self.move_behavior = Walk()
        self.position = 0
  
    def move(self, unit = 1):
        self.position += self.move_behavior.move(unit)



Python基礎訓練營景越Python基礎訓練營QQ羣

在這裏插入圖片描述
歡迎各位同學加羣討論,一起學習,共同成長!

發佈了682 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章