我尝试创建对象树/不是类。我的意思是我不想在原型级别创建对象之间的关系,而是在实例级别创建对象之间的关系,因此将通过代码解决对象之间的关系。
在这里我写了一些东西
class human :
def __init__ (self,Name,Surname,Gender,Age,Father,Mother) :
self.Name = Name
self.Surname = Surname
self.Gender = Gender
self.Age = Age
self.Father = Father
self.Mother = Mother
class family :
def __init__ (self,Surname,Members) :
self.Surname = Surname
self.Members = Members
def ListMembers (self) :
for member in self.Members :
print (self.Surname +' family member ' +member.Name + ' '+ member.Surname + ' is ' + member.Age + " years old ")
human1 = human( 'Mary','Walter','Female','42','Jason White','Sally White')
human2 = human('Charles', 'Walter', 'Male', '45', 'Samuel Ribbon', 'Angie Ribbon')
human3 = human('Claire', 'White', 'Female', '16', 'Charles Walter', 'Mary Walter')
WalterFamily = family('Walter',(human1,human2,human3))
WalterFamily.ListMembers
当我运行此代码时,WalterFamily.ListMembers 什么也不打印。
将对象嵌套在另一个对象中的确切方法是什么?就像我在这里尝试做的那样:在一个家庭中嵌套 3 个人类,并且还可以访问这些对象的属性..