您的位置:首页 > 编程语言 > Python开发

笨办法学python习题40

2018-02-09 17:36 295 查看
python版本:3      若有错误,敬请指出 模块名称:测试.py

#习题40
class TheThing(object):
def __init__(self):
self.number = 0
def some_function(self):
print("I got called.")
def add_me_up(self,more):
self.number += more
return self.number

a = TheThing()
b = TheThing()

a.some_function()
b.some_function()
print("-"*10)
print(a.add_me_up(20))
print(b.add_me_up(30))
print("-"*10)
print(a.number)
print(b.number)

class TheMultiplier(object):
def __init__(self,base):
self.base = base

def do_it(self,m):
return m * self.base
print("-"*10)
x = TheMultiplier(a.number)
print(x.do_it(b.number))
#加分习题已经没办做了...
#创建一个类,类的开头字母要大写
#使用特殊方法__init__()
#形参self 必不可少,还必须位于其他形参的前面
#初始化属性
#将这个实例存储在变量中
运行截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python