Python类层次结构

时间:2015-09-03 16:03:52

标签: python

拥有nex类:

class Person(object):
    name = ""
    arrayTest = []
    def __init__(self, name, item):
        self.name = name
        self.arrayTest.append(item)

    def speak(self):
        print "Hi! My name is ", self.name
        print "And my sons are: "
        print self.arrayTest


basil = Person("Basil", "Sofia")
basil = Person("Basil", "Julia")
basil.speak()

>>> Hi! My name is  Basil
And my sons are:
['Sofia', 'Julia']

polly = Person("Polly", "Peter")
polly.speak()

>>>Hi! My name is  Polly
And my sons are:
['Sofia', 'Julia', 'Peter']

我希望第二个实例只返回Peter作为Polly的儿子,但事实并非如此,

我做错了什么?

谢谢!

0 个答案:

没有答案