python知识捡拾---类方法篇

python的属性分为实例属性和静态属性

实例属性是以self作为前缀的属性
instance实例化对象
内置属性的使用:

#-*-encoding:utf-8-*-
class Fruit:
    price = 0
    def __init__(self):
        self.__color="blue" #私有属性

class Apple(Fruit):        #Apple继承了Fruit
    pass

if __name__=="__main__":
    fruit=Fruit()
    apple=Apple()
    print Apple.__bases__   #输出基类组成的元组
    print apple.__dict__    #输出属性组成的字典 结果为:{'_Fruit__color': 'blue'}
    print apple.__module__  #输出类所在的模块名 结果为:__main__
print apple.__doc__         #输出doc文档

类的方法:
python使用函数staticmethod()或"@ staticmethod"指令的方式把普通的函数转换为静态方法,python的静态方法并没有和类的实例进行名称绑定,python的静态方法相当于全局函数

类方法可以使用classmethod()或"@ classmethod"指令定义
类方法的使用和静态方法十分相似,如果某个方法需要被其他实例共享,同时又需要使用当前实例的属性,则将其定义为类方法

内部类的使用:
在某个内部类定义的类称之为内部类,内部类中的方法可以使用两种方法调用:
一种是直接使用外部类调用内部类,生成内部类的实例,再调用内部类的方法,调用格式如下:

object_name = outclass_name.inclass_name()
object_name.method()

第二种方法是先对外部类进行实例化,然后在实例化内部类,最后再调用内部类方法,调用格式如下:

out_name = outclass_name()
in_name = out_name.inclass_name()
in_name.method()

垃圾回收机制:
python提供了gc模块释放不再使用的对象,垃圾回收的机制有许多种算法
python采用的是引用计数的方式。当某个对象在其作用域内不再被其他对象引用时,python就会自动清除该对象。垃圾回收机制很好地避免了内存泄漏的发生。函数collect()可以一次性收集所有待处理的对象
实例:

#-*-encoding:utf-8-*-
import gc
class Fruit:
    def __init__(self,name,color):
        self.__name = name
        self.__color = color

    def getColor(self):
        return self.__color

    def setColor(self,color):
        self.__color=color

    def getName(self):
        return self.__name

    def setName(self,name):
        self.__name = name

class FruitShop:          #水果店类
    def __init__(self):
        self.fruits = []   #定义了属性fruits,fruits是1个列表,用于存放水果店中的水果

    def addFruit(self,fruit):   #添加水果,把对象fruit添加到fruits列表中
        fruit.parent = self     #把Fruti类关联到FruitShop类,设置fruit对象的parent属性为self.即把FruitShop实例化对象的引用关联到添加的fruit对象上
        self.fruits.append(fruit)

if __name__=="__main__":
    shop = FruitShop()
    shop.addFruit(Fruit("apple","red"))   #向shop对象中添加两个fruit对象
    shop.addFruit(Fruit("banana","yellow"))
    print gc.get_referrers(shop)  #调用函数get_referrers()列出shop对象关联的所有对象
    del shop            #删除shop对象,但是shop对象关联的其他对象并没有释放
    print gc.collect()   #显示调用垃圾回收机制,调用collect()释放shop对象关联的其他对象,collect()返回结果表示释放的对象个数。输出结果为7

类的内置方法:

内置方法说明
init(self,…)初始化对象,在创建新对象时调用
del(self)释放对象,在对象被删除之前调用
new(cls,*args,**kwd)实例的生成操作
str(self)在使用print语句时被调用
getitem(self,key)获取序列的索引key对应的值,等价于seq[key]
len(self)在调用内联函数len()时被调用
cmp(src,dst)比较两个对象src和dst
getattr(s,name)获取属性的值
setattr(s,name,val)设置属性的值
delattr(s,name)删除name属性
getattribute()getattribute()的功能与__getattr__()类似
gt(self,other)判断self对象是否大于other对象
lt(self,other)判断self对象是否小于other对象
ge(self,other)判断self对象是否大于或等于other对象
le(self,other)判断self对象是否小于或等于other对象
eq(self,other)判断self对象是否等于other对象
call(self,*args)把实例对象作为函数调用

new():
它会在__init__()之前被调用,用于生成实例对象。利用这个方法和类属性的特性可以实现设计模式中的单例模式。
单例模式设计的类只能实例化1个对象

#-*-encoding:utf-8-*-
class Singleton(object):
    __instance = None    #定义私有的类属性,__instance的值是第一次创建的实例,以后的实例化操作都将共享这个属性。因此达到了创建唯一实例的目的

    def __init__(self):
        pass

    def __new__(cls, *args, **kwd):#重新实现方法__new__()
        if Singleton.__instance is None: #判断当前的实例是否为"None"如果为"None"则调用父类object的__new__()方法,生成一个唯一实例
            Singleton.__instance=object.__new__(cls,*args,**kwd)
        return Singleton.__instance  #返回唯一实例

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值