如何在 Python 中的 class 方法中增加实例变量
How to increase instance variable in class method in Python
我被困在这个问题中(第二部分)。问题如下:
创建一个名为 AppleBasket 的 class,其构造函数接受两个输入:一个表示颜色的字符串和一个表示苹果数量的数字。构造函数应该初始化两个实例变量:apple_color 和 apple_quantity。编写一个名为 increase 的 class 方法,每次调用时将数量增加 1。您还应该为此 class 编写一个 str 方法,即 returns 格式的字符串:"A basket of [quantity goes here] [color goes here] apples." 例如"A basket of 4 red apples." 或 "A basket of 50 blue apples."
我的代码是:
class AppleBasket():
def __init__(self, apple_color, apple_quantity):
self.apple_color = apple_color
self.apple_quantity = apple_quantity
def getC(self):
return self.apple_color
def getQ(self):
return self.apple_quantity
def __str__(self):
return "A basket of {} {} apples.".format(self.apple_quantity, self.apple_color)
def increase(self):
return self.apple_quantity + 1
print(AppleBasket("Red", 4))
但是,数量仍然是 4 而不是 5。
有人可以告诉我我在做什么错误吗?谢谢。
修正你的增加方法:
def increase(self):
self.apple_quantity += 1
class苹果篮:
def init(self,apple_color,apple_quantity):
self.apple_color=apple_color
self.apple_quantity=apple_quantity
def ap(self):
self.apple_color=apple_color
def apl(self):
self.apple_quantity=apple_quantity
def __str__(self):
return "A basket of {} {} apples.".format(self.apple_quantity,self.apple_color)
def increase(self):
self.apple_quantity += 1
print(AppleBasket("绿色",4))
class AppleBasket():
def ___init__(self, color, quantity):
self.apple_color = color
self.apple_quantity = quantity
def increase(self):
self.apple_quantity += 1
def __str__(self):
return "A basket of {} {} apples.".format(self.apple_quantity, self.apple_color)
我被困在这个问题中(第二部分)。问题如下: 创建一个名为 AppleBasket 的 class,其构造函数接受两个输入:一个表示颜色的字符串和一个表示苹果数量的数字。构造函数应该初始化两个实例变量:apple_color 和 apple_quantity。编写一个名为 increase 的 class 方法,每次调用时将数量增加 1。您还应该为此 class 编写一个 str 方法,即 returns 格式的字符串:"A basket of [quantity goes here] [color goes here] apples." 例如"A basket of 4 red apples." 或 "A basket of 50 blue apples."
我的代码是:
class AppleBasket():
def __init__(self, apple_color, apple_quantity):
self.apple_color = apple_color
self.apple_quantity = apple_quantity
def getC(self):
return self.apple_color
def getQ(self):
return self.apple_quantity
def __str__(self):
return "A basket of {} {} apples.".format(self.apple_quantity, self.apple_color)
def increase(self):
return self.apple_quantity + 1
print(AppleBasket("Red", 4))
但是,数量仍然是 4 而不是 5。
有人可以告诉我我在做什么错误吗?谢谢。
修正你的增加方法:
def increase(self):
self.apple_quantity += 1
class苹果篮: def init(self,apple_color,apple_quantity): self.apple_color=apple_color self.apple_quantity=apple_quantity
def ap(self):
self.apple_color=apple_color
def apl(self):
self.apple_quantity=apple_quantity
def __str__(self):
return "A basket of {} {} apples.".format(self.apple_quantity,self.apple_color)
def increase(self):
self.apple_quantity += 1
print(AppleBasket("绿色",4))
class AppleBasket():
def ___init__(self, color, quantity):
self.apple_color = color
self.apple_quantity = quantity
def increase(self):
self.apple_quantity += 1
def __str__(self):
return "A basket of {} {} apples.".format(self.apple_quantity, self.apple_color)