python学习之--面向对象

#定义一个基类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)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值