| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 
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("我现在的位置是",self.x,self.y) 
 
 
 
class GoldFish(Fish): 
    pass 
 
class Carp(Fish): 
    pass 
 
class Salmon(Fish): 
    pass 
 
 
class Shark(self): 
    def __init__(self): 
       self.hungry = True 
    def eat(self): 
      if self.hungry : 
        print("现在我还没吃饱") 
        self.hungry =False 
      else: 
          print("吃的太饱了") 
           
 
 
 
 
 
 
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license()" for more information. 
>>>  
= RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python37/p11-2.py 
Traceback (most recent call last): 
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python37/p11-2.py", line 22, in <module> 
    class Shark(self): 
NameError: name 'self' is not defined 
>>> 
那不应该是这样:
- 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("我现在的位置是",self.x,self.y)
 
  
 
 
- class GoldFish(Fish):
 
 -     pass
 
  
- class Carp(Fish):
 
 -     pass
 
  
- class Salmon(Fish):
 
 -     pass
 
  
 
- class Shark(Fish):
 
 -     def __init__(self):
 
 -        self.hungry = True
 
 -     def eat(self):
 
 -       if self.hungry :
 
 -         print("现在我还没吃饱")
 
 -         self.hungry =False
 
 -       else:
 
 -           print("吃的太饱了")
 
  复制代码 
 
 
 |   
 
 
 
 |