python面向对象(上)

python面向对象编程[java、C++、php]

  • 封装
  • 继承
  • 多态
如何去定义一个类
class Animal(object):

    #构造器
    def __init__(self,name,age):
        self.name=name
        self.age=age

    #析构器
    def __del__(self):
        print(self.__class__.__name__+' already del')

    def eat(self):
        print(self.__class__.__name__+' eat')

    def move(self):
        print(self.__class__.__name__+' move')

    def sleep(self):
        print(self.__class__.__name__+' sleep')
class Animal(object):

    #构造器
    def __init__(self,name,age):
        self.name=name
        self.age=age

    #析构器
    def __del__(self):
        print(self.__class__.__name__+' already del')

    def eat(self):
        print(self.__class__.__name__+' eat')

    def move(self):
        print(self.__class__.__name__+' move')

    def sleep(self):
        print(self.__class__.__name__+' sleep')

class Person(Animal):
    def __init__(self,name,age,sex):
        Animal.__init__(self,name,age)#子类调用父类的构造方法
        self.sex = sex

    def think(self):
        print(self.__class__.__name__+' think')

    def wear(self):
        print(self.__class__.__name__+ ' wear')

if __name__ == '__main__':
    a=Animal('小轩',2)#对象实例化(),a是一个实例化对象
    print('name=%s'%a.name)
    print('age=%d'%a.age)

    a.sleep()#self不需要传递,由解释器帮你自动完成
    a.eat()
    a.move()

    p=Person('人',30,'男')#实例化Person类型的对象
    print(p.name)
    print(p.age)
    print(p.sex)
    p.think()
    p.wear()

    p.eat()
    p.sleep()
    p.move()
  • 实例化(定义类之后用此类构造出对象)
  • init(self)[构造器]
  • 属性指的数据和方法(脱离类体之外叫函数)
  • self就是指的当前实例化之后的对象
python构造器(不是一个真正的构建对象的构造器,只是实例化对象的回调而已)
#普通函数没有self参数
def func():
    pass

class P():
    def __init__(self):
        print('+'*2)

    def __init__(self,name,age):
        self.name=name
        self.age=age

    def run(self):
        pass

if __name__ == '__main__':
    p=P()#实例化
    p2=P('xxx',20)
对象实例化调用实例
class P():

    def __init__(self,name,age):
        self.name=name
        self.age=age

    def run(self):
        print(self.name+' run...')

if __name__ == '__main__':
    p2=P('ZangzzzzZ',20)
    print(p2.name)
    print(p2.age)
    p2.run()
方法与函数的区别
def run():
    print(' run..')
class P():

    def __init__(self,name,age):
        self.name=name
        self.age=age

    def run(self):
        print(self.name+' run...')

if __name__ == '__main__':
    run()
python可变对象与不可变对象类型
def run():
    print(' run..')

class P():

    has_two_eyes=[]#静态属性

    #def func(self):
    #    print('func invoke...')

    def __init__(self,name,age):
        self.name=name
        self.age=age

    def run(self):
        print(self.name+' run...')

if __name__ == '__main__':
    p1=P('p1',1)#实例化对象
    p2=P('p2',2)#实例化对象
    #print(P.has_two_eyes)
    #print(p1.has_two_eyes)
    #print(p2.has_two_eyes)
    #******************************************
    #P.has_two_eyes+=1#静态属性更新
    #print(P.has_two_eyes)
    #print(p1.has_two_eyes)
    #print(p2.has_two_eyes)
    #p1.has_two_eyes+=1
    #print(P.has_two_eyes)
    #print(id(P.has_two_eyes))
    #print(id(p1.has_two_eyes))
    p1.has_two_eyes.append(123)
    print(id(P.has_two_eyes))
    print(id(p1.has_two_eyes))
    print(P.has_two_eyes)
python动态语言特性体现
class P():

    def __init__(self):
        pass
if __name__ == '__main__':
    p=P()
    p.name='ZangzzzZ'
    P.age=20

    print(p.name)
    print(p.age)

    print(hasattr(p,'name'))
    print(hasattr(p,'age'))
    #print(getattr(p,'xxxx'))
    p.func=lambda x: x+1
    print(p.func(1))
继承与多态
class P():

    def __init__(self,name,age):
        self.name=name
        self.age=age

    def run(self):
        print('P run...')


class C(P):
    def __init__(self,name,age,sex):
        P.__init__(self,name,age)#调用父类构造方法
        self.sex=sex

    def run(self):
        P.run(self)#子类方法体内调用父类方法
        print('C run...')

if __name__ == '__main__':
    c=C('chile', 20 , 'man')
    c.run()#多态
  • 封装

    • 属性私有化
    • 对外提供方法,对属性进行访问和修改
  • python类内部属性单下划线打头(私有属性)、双下划线打头的(私有属性无法访问)、前后都有双下划线(特殊属性)

dir()内置函数用来查看所有类的属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值