笨办法学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 必不可少,还必须位于其他形参的前面
#初始化属性
#将这个实例存储在变量中
运行截图:
#习题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-习题40
- 笨办法学Python-习题37-39
- 笨办法学python习题1第一个程序
- 笨办法学python加分习题30
- 笨办法学python习题6 字符串和文本
- 笨办法学python习题15 读取文件
- 笨办法学Python-习题7,8,9
- 笨办法学python之习题26
- 笨办法学python习题20 函数和文件
- 笨办法学python笔记:习题10 那是什么(转义字符)
- 笨办法学python加分习题24
- 笨办法学Python习题11 提问
- 笨办法学python习题3 数字和数学计算
- 笨办法学Python-习题10
- 笨办法学python(习题39)——items()、get()、
- 笨办法学python习题3数字和数学计算
- 习题 5: 更多的变量和打印 | 笨办法学 Python
- 笨办法学Python习题33:while循环
- 笨办法学Python笔记二——习题5,8
- 笨办法学Python-习题27-31