20181205 类的内置方法、类的继承

类的内置方法

  • 所谓内部类,就是在类的内部定义的类,主要目的是为了更好的抽象现实世界
  • 例子
    汽车是个类,汽车的底盘,轮胎也可以抽象为类,将其定义到汽车类中,则形成内部类,更好的描述汽车类,因为底盘、轮胎是汽车的一部分。

内部类的实例方法

  • 方法1:直接使用外部类调用内部类
    object_name = outclass_name.inclass_name()
  • 方法2:先对外部类进行实例化,然后再实例化内部类
    out_name = outclass_name()
    in_name = out_name.inclass_name()
    in_name.method()
#!/usr/bin/python
#coding=utf-8
class People(object):
    color = 'yellow'
    __age = 30
    
    class Chinese(object):
        name = "I am chinese"
        
jack = People.Chinese() //相当于一个对象
print jack.name // 直接使用外部类调用内部类

or

#!/usr/bin/python
#coding=utf-8
class People(object):
    color = 'yellow'
    __age = 30 

    class Chinese(object):
        name = "I am chinese"

ren = People()
jack = ren.Chinese()
print jack.name

or

print People.Chinese.name// 通过类的方法访问
print People.Chinese().name// 通过对象方法访问的

类的内置方法(魔术方法)

  • str(self)
  • 构造函数与析构函数
    • 构造函数:
      用于初始化类的内部状态,python提供的构造函数是__init__():
      init() 方法是可选的,如果不提供,python会给出一个默认的__init__方法
      • 析构函数:
        用于释放对象占用的资源,Python提供的析构函数是__del__():
        del()也是可选的,如果不提供,则python会在后台提供默认析构函数
#!/usr/bin/python
#coding=utf-8
class People(object):
    color = 'yellow'
    __age = 30


    class Chinese(object):
        name = "I am chinese"
    def __str__(self):
        return "this is pelple class"
    def __init__(self):  //init函数会自动执行,会在对类实例化的时候自动执行
        self.color = 'black'
        self.think()   //初始化函数不需要调用可以直接执行
    def __init__(self, c='white'):  //init函数会自动执行,会在对类实例化的时候自动执行
        self.color = c
    
ren = People()
print ren.color // 调用对象会将值初始化变成black
print People.color// 调用类不会初始化,值不变
jack = People('green')// 通过对象访问值会被接收
print jack.color
#!/usr/bin/python
#coding=utf-8
class People(object):
    color = 'yellow'
    __age = 30

    class Chinese(object):
        name = "I am chinese"
    def __str__(self):
        return "this is pelple class"
    def __init__(self, c='white'):  
        print "Init..."
        self.color = c
        self.think()
        self.fd = open('/etc/passwd')
    def think(self):
        self.color = "black"
        print "I am a %s" % self.color
        print "I am a thinker"
        print self.__age
     def __del__(self):  // 在脚本最后执行delete,主要为了释放资源
         print "delete"
         self.fd.close()
        
jack = People('green')
print People.color
print jack.color
print 'Main end'  

python垃圾回收机制

  • Python采用垃圾回收机制来清理不再使用的对象;python提供gc模块释放不再使用的对象
  • python采用”引用计数“的算法方式来处理回收,即:当某个对象在其作用域内部再被其他对象引用的时候,python就自动清除对象;
  • gc模块的colloct()可以一次性收集所有待处理的对象(gc.collect)
  • print gc.collect 通过collect来计算有没有被回收的内存,如果是0表示没有回收

类的继承

面向对象三个特点封装、继承和多台。
前面都是封装,如何创建一个类,在类里创建属性和方法,内部类。

  • 继承是面向对象的重要特性之一;
  • 继承关系:继承是相对两个类而言的父子关系,子类继承了父类的所有公有属性和方法;
  • 继承实现了代码重用

使用继承

  • 继承可以重用已经存在的数据和行为,减少代码的重复编写。Python在类名后使用一对括号来表示继承关系,括号中的类即为父类。
  • class Myclass(ParentClass)
    如果父类定义了__init__方法,子类必须显示调用父类的__init__方法;
    ParentClass.init(self,[args…])
    如果子类需要扩展父类的行为,可以添加__init__方法的参数
例1:
#!/usr/bin/python
#coding=utf-8
class People(object):  //定义类的时候new style带有(object),传统方法没有(object)
    color = 'yellow'

    def __init__(self):
        print "init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s" % self.color
        print "I am a thinker"

class Chinese(People): //子类只要调用父类,那么父类里构造函数init会执行
    pass

cn = Chinese()
print cn.dwell
cn.think()

例2:
#!/usr/bin/python
#coding=utf-8
class People(object):
    color = 'yellow'

    def __init__(self, c): // 如果父类有多个参数,子类需要使用如下写法去调用
        print "init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s" % self.color
        print "I am a thinker"

class Chinese(People):
    def __init__(self):
        People.__init__(self, 'red')
    pass

super函数继承父类

在父类中有构造函数,在子类中也需要通过构造函数继承过来。

class A(object):
    def __init__(self):
        print "enter A"
        print "leave A"
        
class B(A):
    def __init__(self):
        print "enter B"
        super(B,self).__init__()
        print "leave B"
b =B()

例1:
#!/usr/bin/python
#coding=utf-8
class People(object):
    color = 'yellow'

    def __init__(self, c):
        print "init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s" % self.color
        print "I am a thinker"

class Chinese(People):
    def __init__(self):
       super(Chinese, self).__init__('red')
   def think(self):    //修改父类方法,调用后直接调用这个方法
       print "i like talking"
    pass

cn = Chinese()

注:super只支持new style 定义类的时候需要加上object class People(object)

多重继承

  • Python支持多重继承,即一个类可以继承多个父类;
  • 语法
    class class_name(Parent_c1, Parent_c2,…)
  • 注意:
    当父类中出现多个自定义的__init__方法时,多重继承只执行第一个类的__init__方法,其他不执行。
#!/usr/bin/python
#coding=utf-8
class People(object):
    color = 'yellow'

    def __init__(self):
        print "init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s" % self.color
        print "my home in %s" % self.dwell
class Martian(object):
    color = 'red'

    def __init__(self):
        self.dwell = 'Martian'

class Chinese(People, Martian):
    def __init__(self):
        People.__init__(self) // 调用People 类里的构造函数,默认有限调用父类前面类的构造init函数

cn = Chinese()
cn.think()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值