笨办法学python习题40
程序员文章站
2022-07-01 15:38:13
...
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 - 习题5: More Variables and Printing
-
笨办法学Python - 习题8-10: Printing & Printing, Printing
-
笨办法学Python - 习题3: Numbers and Math
-
笨办法学python3 学习笔记 习题26
-
笨办法学python3 学习笔记 习题25
-
笨办法学python3 笔记 习题24
-
《笨办法学Python》习题24、25
-
Python自学之旅 #新手 #MacBook #《“笨办法”学Python》#第四章:言归正传讲Python:Mac系统的终端Terminal命令行快速入门之较复杂的命令
-
笨办法学Python3 习题47-48
-
笨办法学Python3 习题18和19