您的位置:首页 > 编程语言 > Python开发

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: