在这段代码中,有一个Person
类有一个属性name
,它在构造对象时被设置。
它应该这样做:
- 创建类的实例时,属性被正确设置
- 调用该
greeting()
方法时,问候语会说明分配的名称。
class Person:
def __init__(self, name):
self.name = name
def greeting(self):
# Should return "hi, my name is " followed by the name of the Person.
return "hi, my name is {}".format(self.name)
# Create a new instance with a name of your choice
some_person = "xyz"
# Call the greeting method
print(some_person.greeting())
它返回了错误:
AttributeError:“str”对象没有属性“greeting”