类方法getter出错 - Python

时间:2013-12-04 10:43:14

标签: python class oop

我的方法getter在获取电话号码方面遇到了一些麻烦。这是我的代码:

# Personal info class
class PersonalInfo:
    def set_names(self, name):
        self.__names = name

    def set_addresses(self, add):
        self.__addresses = add

    def set_ages(self, age):
        self.__ages = age

    def set_numbers(self, number):
        self.__numbers = number

    # Accessor methods    
    def get_names(self):
        return self.__names

    def get_addresses(self):
        return self.__addresses

    def get_ages(self):
        return self.__ages

    def get_numbers(self):
        return self.__numbers

def main():
    # references PersonalInfo object
    info = PersonalInfo()

    # stores values in the object
    info.set_names("Will")
    info.set_addresses("26 Explorer Blvd, Punta Gorda, FL")
    info.set_ages("21")
    info.set_numbers("941-245-0255")

    # displays values stored in object's fields
    print "Name:", info.get_names()
    print "Address:", info.get_addresses()
    print "Age:", info.get_ages()
    print "Phone number:", info.get_numbers
    print

    # stores new values
    info.set_names("Pat")
    info.set_addresses("26 Explorer, Punta Gorda, FL")
    info.set_ages("28")
    info.set_numbers("941-245-0266")

    # displays new values
    print "Name:", info.get_names()
    print "Address:", info.get_addresses()
    print "Age:", info.get_ages()
    print "Phone number:", info.get_numbers
    print 

    # stores new values again
    info.set_names("Motley")
    info.set_addresses("11 Scotland Ave, Edison, NJ")
    info.set_ages("49")
    info.set_numbers("732-709-3409")

    # displays newest values
    print "Name:", info.get_names()
    print "Address:", info.get_addresses()
    print "Age:", info.get_ages()
    print "Phone number:", info.get_numbers

main()

当我运行此代码时,对于所有电话号码,它会显示“main.PersonalInfo instance in 0x0297BB70>>”。这是介绍计算机编程课程,老实说我不明白我哪里出错了。任何帮助,将不胜感激。 :)

1 个答案:

答案 0 :(得分:4)

你忘了实际调用方法。注意parens:

print "Phone number:", info.get_numbers()