| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 本帖最后由 grapegirl 于 2016-7-8 09:12 编辑  
 
import random as r 
 
class Fish: 
    def __init__(self): 
        self.x = r.randint(0,10) 
        self.y = r.randint(0,10) 
 
    def move(self): 
        self.x -= 1 
        print('my location is:',self.x,self.y) 
 
class Goldfish(Fish): 
    pass 
 
class Carp(Fish): 
    pass 
 
class Salmon(Fish): 
    pass 
 
class Shark(Fish): 
    def __init__(self): 
        self.hunger = True 
 
    def eat(self): 
        if self.hunger: 
            print('cicici') 
            self.hunger = False 
        else: 
            print('full!!!') 
 
>>> f = Fish() 
>>> f.move() 
Traceback (most recent call last): 
  File "<pyshell#6>", line 1, in <module> 
    f.move() 
  File "H:/电影/小甲鱼—《零基础入门学习Python》/code/little programs/fish_inherit.py", line 9, in move 
    self.x = self.x - 1 
AttributeError: 'Fish' object has no attribute 'x' |   
 
 
 
 |