|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 课堂笔记:
- 1. __init__函数,是初始化数据的函数,不是第一个调用的,返回None,返回其他会报错.
- 2. __new__函数,是第一个调用的,继承的父类属性不可修改(不可用init时),使用__new__先修改,然后传到父类的__new__里面.
- 3. __del__函数,__del__是在没有变量指向自己的时候被调用.
- 测试题:
- 0.
- 变量前后都有__的函数
- 1.
- __new__():,它的第一个参数不是self而是这个类cls,而他的参数会传给init
- 2.
- 当类里有数据需要初始化的时候.
- 3.
- __init__不能返回除None以外的值.
- 4.
- 当没有变量指向指向这个类时.
- 动动手:
- 0.
- class FileObject():
- def __del__(self):
- self.f.close()
- 1.
- class C2F():
- def __new__(cls,temp):
- return temp * 1.8 +32
- 2.
- class Nint(int):
- def __new__(cls,temp):
- if isinstance(temp,str):
- total = 0
- for each in temp:
- total += ord(each)
- temp = total
- return int.__new__(cls,total)
- 3.
- class Nstr():
- def __new__(cls,temp):
- s = str(temp)
- total = 0
- for each in s:
- total += ord(each)
- return total
复制代码 |
|