classDog:
def__init__(self):
#私有属性,以双下划线开头,这样就没法以点的形式访问了
#这个属性只能用self.来访问,不能用dog.访问
self.__age =10
self.name ="shit"
defget_age(self):
returnself.__age
defset_age(self,age):
self.__age = age
defwant_fuck(self,money):
ifmoney >200:
self.__fuck()
else:
print("fuck yourself")
def__fuck(self):
"""私有方法,同样以双下划线开头"""
print("this is a private func,fuck you")
def__del__(self):
"""魔术方法,对象被真正销毁之前,真正的销毁才会回调这个,包括手动或者系统销毁"""
print("dog is dead")
def__str__(self):
return"live dog"
importtime
dog = Dog()
dog.set_age(44)
#调用私有变量
print(dog.get_age())
print(dog.name)
#调用私有方法
dog.want_fuck(300)
dog1 = dog
#实测,这个方法会删除对象,但是如果有多个引用,它只会删除某个引用,而不是真正销毁对象
# del dog
# time.sleep(5)
# print(dog)
importsys
#计算某对象的引用个数,但是它本身也会使引用个数加1
count = sys.getrefcount(dog1)
print(count)