gpt4 book ai didi

python - 基本 Python 类

转载 作者:太空宇宙 更新时间:2023-11-04 08:18:15 25 4
gpt4 key购买 nike

我只是尝试构建一个基础类,以便我可以学习更多有关 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__ 中的bmitotalnotes 变量将在本地并在__init__ 完成时被垃圾回收,因此初始化他们那样是没用的。您可能希望将它们初始化为 self.notesself.bmitotal

Bodymassindex.count一个静态变量,它与所有实例共享它的值。

关于python - 基本 Python 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com