反射机制: 让对象告诉我们它的相关信息(对象拥有的属性和方法, 对象所属的类, 这个类是否有某个属性或者方法等等)
1). 对象拥有的属性和方法
li = [1,2,3,4]
print(dir(li))
print(help(li.index))
import random
class Turtle(object):
"""
乌龟类
"""
# 构造函数什么时候执行? =---=====创建对象时执行
def __init__(self): # self指的是实例化的对象;
# 乌龟的属性: x,y轴坐标和体力值
# 乌龟的x轴, 范围1,10
self.x = random.randint(1, 10)
self.y = random.randint(1, 10)
# 乌龟初始化体力为100
self.power = 100
# 类的方法:
def move(self):
# 乌龟的最大移动能力为2,[-2, -1, 0, 1, 2]
move_skill = [-2, -1, 0, 1, 2]
# 计算出乌龟的新坐标(10, 12)
new_x = self.x + random.choice(move_skill)
new_y = self.y + random.choice(move_skill)
# 对于新坐标进行检验, 是哦否合法, 如果不合法, 进行处理
self.x = self.is_vaild(new_x)
self.y = self.is_vaild(new_y)
# 乌龟每移动一次,体力消耗1
self.power -= 1
def is_vaild(self, value):
"""
判断传进来的