|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 课堂笔记:
- 1. int() str() 等这种类型是工厂函数,实质是类对象.
- 2. + __add__,- __sub__,* __mul__,/ __truediv__,// __floordiv__,% __mod__,__divmod__,** __pow__(self,other[,modulo]),<< __lshift__,>> __rshift__,&__and__,^ __xor__,| __or__.
- 3. 改写函数,注意,self,other可以看做实例化后的a,b.ab的方法都可以在改写的过程中用到.
- 测试题:
- 0.
- 工厂函数就是类对象
- 1.
- __add__(self,other)方法
- 2.
- 母鸡噢
- 类的属性和方法名不能一样,否则在调用方法的时候会调用属性(),从而报错.
- 3.
- + __add__
- - __sub__
- * __mul__
- / __truediv__
- // __floordiv__
- % __mod__
- divmod(a,b) __divmod__
- ** __pow__
- << __lshift__
- >> __rshift__
- & __and__
- | __or__
- ^ __xor__
- 4.
- 面向对象的编程?
- 支持'鸭子类型'
- 动动手:
- 0.
- class Nstr(str):
- def __sub__(self,other):
- s = str(self)
- o = str(other)
- while o in s:
- s = s.strip(o)
- print(s,o)
- return s
- 1.
- class Nstr(str):
- def __lshift__(self,other):
- s = str(self)
- o = int(other)
- return s[o:] + s[:o]
- def __rshift__(self,other):
- s = str(self)
- o = int(other)
- return s[-o:] + s[:-o]
- 2.
- class Nstr():
- def __new__(cls,temp):
- s = str(temp)
- total = 0
- for each in s:
- total += ord(each)
- return total
复制代码 |
|