|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
2. 请问如何在继承的类中调用基类的方法?
- class Derived(Base):
- def meth (self):
- super(Derived, self).meth()
复制代码
请问super函数为何是这么写?括号里这两个参数如何理解??看不懂。。。
5. 尝试自己举例说明如何使用类的静态方法,并指出使用类的静态方法有何有点和需要注意的地方?
- class C:
- @staticmethod # 该修饰符表示 static() 是静态方法
- def static(arg1, arg2, arg3):
- print(arg1, arg2, arg3, arg1 + arg2 + arg3)
- def nostatic(self):
- print("I'm the f**king normal method!")
复制代码
- >>> c1 = C()
- >>> c2 = C()
- # 静态方法只在内存中生成一个,节省开销
- >>> c1.static is C.static
- True
- >>> c1.nostatic is C.nostatic
- False
- >>> c1.static
- <function C.static at 0x03001420>
- >>> c2.static
- <function C.static at 0x03001420>
- >>> C.static
- <function C.static at 0x03001420>
- # 普通方法每个实例对象都拥有独立的一个,开销较大
- >>> c1.nostatic
- <bound method C.nostatic of <__main__.C object at 0x03010590>>
- >>> c2.nostatic
- <bound method C.nostatic of <__main__.C object at 0x032809D0>>
- >>> C.nostatic
- <function C.nostatic at 0x0328D2B8>
复制代码
这边静态方法完全不理解啊。。。这串代码在讲啥?然后静态方法只在内存中生成一个是啥意思? |
|