Python面向对象3

一、内部类

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

二、魔术方法(构造函数和析构函数)

 1 #!usr/bin/python
 2 #coding:utf8
 3 
 4 class Milo():
 5     class Test():
 6         var1 = "neibulei"
 7     name = 'csvt'
 8 
 9     def __init__(self,n = 'baby'):
10         self.name = n
11         print "initializing......"
12 
13     def fun1(self):
14         print self.name
15         print 'public'
16         self.__fun2()
17     def __fun2(self):
18         print 'private'
19 
20     @classmethod
21     def classFun(self):
22         print 'class'
23 
24     @staticmethod
25     def staticFun(self):
26         print 'static'
27 
28     def __del__(self):
29         print 'releasing sources......'
30 
31 zou = Milo()

三、垃圾回收机制

Python采用垃圾回收机制清理不再使用的对象;

Python提供gc模块释放不再使用的对象;

Python采用“引用计数”的算法方式来处理回收,即:当某个对象在其作用域内不再被其他对象引用的时候,Python就自动清除对象;

Python的函数collect()可以一次性收集所有待处理的对象(gc.collect())。

原文地址:https://www.cnblogs.com/whp2011/p/3595197.html