8.Python基础 面向对象的基本概念

本文通过Python代码示例展示了面向对象编程的基本概念,包括类的定义、继承、属性和方法等。并通过具体例子说明了如何使用类创建对象及调用其方法。

vamei前辈博客:

http://www.cnblogs.com/vamei/archive/2012/06/02/2531515.html

自己的学习笔记:

#!/usr/bin/python
class Bird(object):  #Bird类继承之object,object是各个类的父类
    have_feather = True
    way_of_reproduction = 'egg'

    def move(self, dx, dy):  #行为方法:move,第一个参数必须是self,其实改成xx也能运行
        position = [0, 0]    #self是供内部使用的
        position[0] += dx
        position[1] += dy
        return position

summer = Bird()
print (summer.way_of_reproduction)
print ('after move:',summer.move(5, 6))


class Chicken(Bird):  #Chicken继承之Bird,它有Bird的所有属性
    way_of_move = 'walk'  #又增加了way_of_move和possible_in_KFC属性
    possible_in_KFC = True
class Oriole(Bird):
    way_of_move = 'fly'
    possible_in_KFC = False

summer = Chicken()
print (summer.have_feather)
print (summer.move(5, 8))

和C++同样具有属性和方法,有继承的属性,还有封装和多态属性吗?


转载于:https://www.cnblogs.com/v-BigdoG-v/p/7398643.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值