#!/usr/bin/python
#-*- coding:utf8 -*-
import gc
class Student:
def __init__(self, name="", age=""):
self._name = name
self._age = age
#print self._name
def __del__(self):
self._name = ""
self._age = ""
def getName(self):
return self._name
def setName(self, name):
self._name = name
def setAge(self, age):
self._age = age
def getAge(self):
return self._age
class SmallStudent:
def __init__(self):
self._stu = []
def addStu(self, stu):
self._stu.append(stu)
smallStu = SmallStudent()
smallStu.addStu(Student("Jack", "21"))
smallStu.addStu(Student("Smith", "23"))
print gc.get_referrers(smallStu) #打印关联的一些信息
del smallStu
print gc.collect() #显示调用垃圾回收
#stu = Student("Jack", "21")
#print stu.getName() #由于函数生命周期的原因 还是会返回Jack
#del stu #会出发__del__操作
#python还有来及回收器 gc
#__call__方法
class Fruit:
class Apple:
def __call__(self):
print "__call__"
app = Apple()
fruit = Fruit()
fruit.app() #两种调用方式
Fruit.app()
class Fruit:
def __getitem__(self, index):
return self.item[index]
fruit = Fruit()
fruit.item = ["apple", "pear", "watermelon"]
for i in fruit:
print i," ",
#python还有个比较好玩的 动态方法
class Fruit:
def getName(self):
return "pear"
def getFruitName(self): #注意使用了self
return "apple"
def getName1(): #注意没有self
return "watermelon"
#一个是添加方法 一个是改变方法名
print
print
print
Fruit.getFruitName = getFruitName
fru = Fruit()
print fru.getFruitName()
print fru.getName()
fru.getName = getName1
#-*- coding:utf8 -*-
import gc
class Student:
def __init__(self, name="", age=""):
self._name = name
self._age = age
#print self._name
def __del__(self):
self._name = ""
self._age = ""
def getName(self):
return self._name
def setName(self, name):
self._name = name
def setAge(self, age):
self._age = age
def getAge(self):
return self._age
class SmallStudent:
def __init__(self):
self._stu = []
def addStu(self, stu):
self._stu.append(stu)
smallStu = SmallStudent()
smallStu.addStu(Student("Jack", "21"))
smallStu.addStu(Student("Smith", "23"))
print gc.get_referrers(smallStu) #打印关联的一些信息
del smallStu
print gc.collect() #显示调用垃圾回收
#stu = Student("Jack", "21")
#print stu.getName() #由于函数生命周期的原因 还是会返回Jack
#del stu #会出发__del__操作
#python还有来及回收器 gc
#__call__方法
class Fruit:
class Apple:
def __call__(self):
print "__call__"
app = Apple()
fruit = Fruit()
fruit.app() #两种调用方式
Fruit.app()
class Fruit:
def __getitem__(self, index):
return self.item[index]
fruit = Fruit()
fruit.item = ["apple", "pear", "watermelon"]
for i in fruit:
print i," ",
#python还有个比较好玩的 动态方法
class Fruit:
def getName(self):
return "pear"
def getFruitName(self): #注意使用了self
return "apple"
def getName1(): #注意没有self
return "watermelon"
#一个是添加方法 一个是改变方法名
Fruit.getFruitName = getFruitName
fru = Fruit()
print fru.getFruitName()
print fru.getName()
fru.getName = getName1
print fru.getName()
QQ交流群: 204944806