我只是尝试构建一个基础类,以便我可以学习更多有关 python 的知识。 到目前为止,我有以下内容:
class Bodymassindex:
count = 0
def __init__(self,name,weight,height):
self.name = name
self.weight = 14 * weight
self.height = 12 * height
notes = "no notes have been assigned yet"
bmitotal = 0
Bodymassindex.count += 1
def displayCount(self):
print "Total number of objects is %d" % Bodymassindex.count
def notesBmi(self,text):
self.notes = text
def calcBmi(self):
return ( self.weight * 703 ) / ( self.height ** 2 )
在添加注释变量和查看方面,正确的做法是什么?
谢谢,
请您参考如下方法:
__init__ 中的bmitotal 和notes 变量将在本地并在__init__ 完成时被垃圾回收,因此初始化他们那样是没用的。您可能希望将它们初始化为 self.notes 和 self.bmitotal
Bodymassindex.count 将像一个静态变量,它与所有实例共享它的值。
