- 论坛徽章:
- 1
|
给一个我设计的:
- import random
- class Person:
- def __init__(self, gender, name=None ):
- self.gender = gender
- self.name = name
- self.father = None
- self.mother = None
- self.bloodline = None
- def give_birth(self, spouse):
- if( self.gender == spouse.gender ):
- return None
- print "%s and %s give birth a baby..." % ( self.name, spouse.name )
- gender = random.choice( ['male', 'female'] )
- baby = Person(gender)
- if gender == 'male':
- print 'It is a boy.'
- else:
- print 'It is a girl.'
- if self.gender == 'male':
- baby.father = self
- baby.mother = spouse
- else:
- baby.father = spouse
- baby.mother = self
- baby.bloodline = baby.father.bloodline
- print
- return baby
- def naming(self, name):
- self.name = name
- def hello_world(self):
- print 'My name is', self.name
- if self.father != None:
- print 'My father is', self.father.name
- if self.mother != None:
- print 'My mother is', self.mother.name
- print
- if __name__ == '__main__':
- alpha = Person( 'male', 'Po' )
- beta = Person( 'female', 'Ling' )
- alpha.hello_world()
- beta.hello_world()
- gamma = alpha.give_birth( beta )
- gamma.naming('Kyle')
- gamma.hello_world()
复制代码
输出:
- D:\MoChou>ttt
- My name is Po
- My name is Ling
- Po and Ling give birth a baby...
- It is a boy.
- My name is Kyle
- My father is Po
- My mother is Ling
复制代码
[ 本帖最后由 flw 于 2007-4-5 17:13 编辑 ] |
|