2018-01-13 类的内置方法

所谓内部类,就是在类的内部定义的类,主要目的是为了更好的抽象现实世界

内部类的实例化方法

方法1:直接使用外部类调用内部类

object_name=putclass_name.inclass_name()

class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
jack=People.Chinese()
print jack.name

方法2:先对外部类进行实例化,然后在实例化内部类

out_name=outclass_name()

in_name=out_name.inclass_name()

in_name.method()

class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
ren=People()
jack=ren.Chinese()
print jack.name
3.通过类名来访问属性

class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
print People.Chinese.name通过类访问属性name
class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
print People.Chinese().name通过对象来访问属性name

魔术方法

1.__str__(self) 

class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
    def __str__(self):
        return "this is people class"
ren=People()
print ren

构造函数与析构函数

构造函数:用于初始化类的内部状态,python 提供的构造函数是__init__():

__init__()方法是可选的,如果不提供,Python会给出一个默认的__init__方法

把类进行实例化的时候自动执行__init__()函数

class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
    def __str__(self):
        return "this is people class"
    def __init__(self):
        self.color='black'

ren=People()
print ren.color
print People.color

class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
    def __str__(self):
        return "this is people class"
    def __init__(self,c='whilte'):
        self.color=c

ren=People()
print ren.color
print People.color
class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
    def __str__(self):
        return "this is people class"
    def __init__(self,c='whilte'):
        self.color=c

jack=People('green')
print jack.color
print People.color
根据对象访问的值是会变的,而根据类访问的属性是不会变的。

析构函数:用于释放对象占用的资源,Python提供的析构函数是__del__();

__del__()也是可选的,如果不提供,则python 会在后台提供默认析构函数

import gc
class People(object):
    color='yellow'
    __age=30
    class Chinese(object):
        #print "i an chiese"
        name="i am chinese"
    def __str__(self):
        return "this is people class"
    def __init__(self,c='whilte'):
        print "init**********"
        self.color=c
        self.think()

    def think(self):
        self.color='black'
        print "i am a %s" % self.color
        print "i am a thinker"
        print self.__age
    def __del__(self):
        print "delete********"
        # self.fd.close()



print gc.collect()
jack=People('green')
print jack.color
print People.color
print "main end"



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值