鱼C论坛

 找回密码
 立即注册
查看: 1621|回复: 2

[技术交流] 零基础入门学习Python 第 37 讲 类和对象 面向对象编程

[复制链接]
发表于 2018-4-4 12:02:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. 课堂笔记:
  2.   1. self:
  3.       a. self把对象名传进类里,访问类里的数据
  4.   2. 公有,私有:
  5.       a. 类里的属性和方法都是公有的
  6.       b. 私有变量用__变量名定义,对象名.__私有变量名访问,或对象名._类名__私有变量名
  7.   3. __init()__方法,被称作魔法方法.在类里定义一个init方法,那么就可以在实例化的时候,对对象进行初始化.

  8. 测试题:
  9. 0.
  10. 封装
  11. 多态,对同一类不同对象调用不同方法,产生不同结果.
  12. 1.
  13. 继承,class A(class B):pass,这时候A就继承了B类,A就是B的子类.
  14. 2.
  15. 把类的对象传入类中做运算.
  16. 3.
  17. 在属性或方法前面加__.
  18. 4.
  19. __init__方法.
  20. 5.
  21. 定义了一个参数self,但是没用这个参数.去掉self就好了.

  22. 动动手:
  23. 0.
  24. import random

  25. class Turtle:
  26.    
  27.     health = 100
  28.     x = random.randint(0,10)
  29.     y = random.randint(0,10)
  30.     direction = random.randint(1,4)

  31.     def moveUp(self):
  32.         y += random.randint(1,2)
  33.         if self.y >= 10:
  34.             self.moveDown()
  35.     def moveDown(self):
  36.         y -= random.randint(1,2)
  37.         if self.y <= 0:
  38.             self.moveUp(self)
  39.     def moveRight(self):
  40.         x += random.randint(1,2)
  41.         if self.x >= 10:
  42.             self.moveLeft()
  43.     def moveLeft(self):
  44.         x -= random.randint(1,2)
  45.         if self.x <= 0:
  46.             self.moveRight()
  47.             
  48.     def move(self):
  49.         self.health -= 1
  50.         if self.direction == 1:
  51.             self.moveUp()
  52.         elif self.direction == 2:
  53.             self.moveDown()
  54.         elif self.direction == 3:
  55.             self.moveRight()
  56.         else:
  57.             self.moveLeft()
  58.         
  59. class Fish:
  60.    
  61.     x = random.randint(0,10)
  62.     y = random.randint(0,10)
  63.     direction = random.randint(1,4)

  64.     def moveUp(self):
  65.         y += 1
  66.         if self.y >= 10:
  67.             self.moveDown()
  68.     def moveDown(self):
  69.         y -= 1
  70.         if self.y <= 0:
  71.             self.moveUp(self)
  72.     def moveRight(self):
  73.         x += 1
  74.         if self.x >= 10:
  75.             self.moveLeft()
  76.     def moveLeft(self):
  77.         x -= 1
  78.         if self.x <= 0:
  79.             self.moveRight()

  80.     def move(self):
  81.         if self.direction == 1:
  82.             self.moveUp()
  83.         elif self.direction == 2:
  84.             self.moveDown()
  85.         elif self.direction == 3:
  86.             self.moveRight()
  87.         else:
  88.             self.moveLeft()



  89. turtle = Turtle()
  90. fish = []
  91. for each_fish in range(10):
  92.     afish = Fish()
  93.     fish.append(afish)

  94. while 1:
  95.     turtle.move()
  96.     print('乌龟坐标是:%d,%d,体力是%d' % (turtle.x,turtle.y,turtle.health))
  97.     for each in range(len(fish)):
  98.         fish[each].move
  99.         print('鱼%d的坐标是:%d,%d' %d (each,fish[each].x,fish[each].y))
  100.         if turtle.x == fish[each].x and turtle.y == fish[each].y:
  101.             turtle.health += 20
  102.             print('鱼%d被吃了,乌龟的体力+20点,现在为%d点' %  (each,turtle.health))
  103.             fish.remove(fish[each])
  104.     if fish == [] or turtle.health == 0:
  105.         break
  106. print('游戏结束')
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-17 14:10:56 | 显示全部楼层
楼主,程序运行时老报x和y局部变量错误。我把x,y用nonlocal和global声明都试了一下,还是没法解决。
怎么破
  1. UnboundLocalError: local variable 'y' referenced before assignment
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-17 15:23:11 | 显示全部楼层
高渐飞 发表于 2018-7-17 14:10
楼主,程序运行时老报x和y局部变量错误。我把x,y用nonlocal和global声明都试了一下,还是没法解决。
怎么 ...

看看是不是函数的包含问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 07:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表