Python基础08 面向对象

本文通过具体的Python代码示例,介绍了类的定义、属性、方法以及子类如何继承父类的特性。通过鸟、鸡和黄鹂的例子,展示了Python中类的创建和继承的实现方式。
class Bird(object):
    have_feather = True
    way_of_reproduction = 'egg'

    def move(self, x, y):
        position = [0, 0]
        position[0] = position[0] + x
        position[1] = position[1] + y
        return position


summer = Bird()
print(summer.way_of_reproduction)
# 调用move方法的时候,我们只传递了dx和dy两个参数,不需要传递self参数(因为self只是为了内部使用)
print("after move: ", summer.move(4, 5))


# 继承
# 鸡
class Chicken(Bird):
    way_of_move = 'walk'
    possible_in_KFC = True


# 黄鹂
class Oriole(Bird):
    way_of_move = 'fly'
    possible_in_KFC = False


chicken = Chicken()
oriole = Oriole()
print(chicken.have_feather)
print(chicken.move(5, 8))
print(oriole.way_of_move)
print(oriole.move(7, 8))

 

转载于:https://my.oschina.net/grittan/blog/2967427

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值