班级/自我问题

2024-05-23 20:38:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码是:

class Pop(object):
    def holder(self):
        self.boobs = 16
        self.sent = "pop"
    def together(self):
        print "%s : %i" % (self.sent, self.boobs)

pop = Pop()

pop.together()

这不是应该印“16岁”吗?很抱歉有奇怪的变量名:P

而且,我是新手。谢谢。在


Tags: 代码selfobjectdefpopclasssentprint
1条回答
网友
1楼 · 发布于 2024-05-23 20:38:39

在您的示例中,您应该首先调用holder,因为这会将变量设置为16。 我想你是想这么做的:

class Pop(object):
    def __init__(self):
        self.boobs = 16
        self.sent = "pop"
    def together(self):
        print "%s : %i" % (self.sent, self.boobs)

pop = Pop()

pop.together()

相关问题 更多 >