面向对象3-Pokemon demo
- 1.可迭代类对象
1.可迭代类对象
想要实现类对象中某些属性/数值的迭代访问,需要在类中定义一个__iter__()方法,和__next__() 的方法(python 2 中为next()方法)。
_iter_()返回值是self,
_next_()中遍历完所有的元素后发起一个StopIteration异常。
# 20210413 类代码实例class Pokemon(object):def __init__(self, name, gender, level, type, status):self.__type = typeself.__gender = genderself.__name = nameself.__level = levelself.__status = statusself.__info = [self.__name, self.__type, self.__gender, self.__level, self.__status]self.__index = -1def getName(self):return self.__namedef getGender(self):return self.__genderdef getType(self):return self.__typedef getStatus(self):return self.__statusdef lever_up(self):self.__status = [s+1 for s in self.__status]self.__status[0] += 1 # 依据具体的升级规则确定def __iter__(self):# 返回一个拥有next方法的对象# 可以实现在容器中逐一访问元素print('名字 属性 性别 级别 等级 能力')return self# def next(self): # python 2.0的写法def __next__(self): # python 3.0的写法if self.__index == len(self.__info)-1:raise StopIterationelse:self.__index += 1return self.__info[self.__index]class Charmander(Pokemon):def __init__(self, name, gender, level):self.__type = ('fire', None)self.__gender = genderself.__name = nameself.__level = levelself.__status = [10+2*level, 5+1*level, 5+1*level, 5+1*level, 5+1*level, 5+1*level]Pokemon.__init__(self, self.__name, self.__gender, self.__level, self.__type, self.__status)pokemon1 = Charmander("Bang", 'male', 5)
print(pokemon1.getGender())# 为私有化
# print(pokemon1.__type) # 报错:'Charmander' object has no attribute '__type'
print(pokemon1._Charmander__type)pokemon2 = Pokemon("abc", "male", 5, 'fire', 0)
for info in pokemon2:print(info)# for 循环的迭代机制:在容器对象上调用了iter()方法,
# iter 方法返回一个对象,该对象封装了next()方法,用于遍历封装内容
# for info in pokemon1():
# print(info)""" for info in pokemon1():
TypeError: 'Charmander' object is not callable
难道是因为没有重写么?
"""