python学习之--面向对象
2015-03-20 16:26
399 查看
#定义一个基类Bird 类属性have_feather ,way_of_reproduction,初始化方法__init__() ,方法move() #定义两个子类 Chicken 和 Oriole 添加属性way_of_move and possible_in_KFC class Bird(object): have_feather = True way_of_reproduction='egg' def __init__(self,more_words): print('We are happy birds.',more_words) def move(self,dx,dy): position = [0,0] position[0] = position[0]+dx position[1] = position[1]+dy return position class Chicken(Bird): way_of_move = 'walk' possible_in_KFC = True class Oriole(Bird): way_of_move = 'fly' possible_in_KFC = False summer = Bird("I am a happy bird") print(summer.way_of_reproduction) print('after move',summer.move(5,8)) summer = Chicken("I am a happy chicken") print(summer.have_feather) print('after move',summer.move(4,7)) print(summer.way_of_move) summer = Oriole("I am a happy Oriole") print(summer.have_feather) print('after move',summer.move(3,10)) print(summer.way_of_move)
#定义一个类Human 类属性laugh ,list:test #初始化函数,方法printGender,show_laugh,laugh_10th class Human(object): laugh = 1 test = ['haha','hehe'] def __init__(self,input_gender): self.gender = input_gender def printGender(self): print(self.gender) def show_laugh(self): print(self.laugh) def laugh_10th(self): for i in range(10): self.show_laugh() lilei = Human('male') lilei.show_laugh() Human.laugh += 1 viease = Human('zhangsan') viease.show_laugh() Human.test[0] = 'enen' print(lilei.test) lilei.test[0] = 'heihei' print(viease.test)
相关文章推荐
- Dive into python 第5和6章面向对象学习笔记
- Python学习笔记(四):面向对象、正则表达式(快速入门篇)
- Python面向对象高级编程——学习笔记
- python第六天学习记录——面向对象基础
- 【python学习】多线程 与 面向对象 结合的一个经典例子
- Python菜鸟学习手册09----面向对象续
- Python学习笔记(2)--面向对象
- 廖雪峰python学习笔记8:面向对象初步
- Python学习一(面向对象和函数式编程)
- [Python学习笔记][第六章Python面向对象程序设计]
- python学习笔记(七) - 面向对象高级编程
- Python面向对象学习资料汇总
- Python面向对象学习(2)
- Python学习----面向对象高级编程
- Python学习(18)面向对象
- python学习小结9:面向对象
- Python面向对象学习
- Python面向对象学习(3):
- python学习笔记六之面向对象相关下(基础篇)
- python学习第十五天 -面向对象之继承和多态