如何搜索对象数组,并根据用户输入的ID返回并打印所有相关属性。 PS:您可以忽略大部分代码,只需滚动到相关代码的最后部分即可。
TheArray是存储对象的数组
示例代码:
class User():
def __init__(self,Number,Name):
ID = Number
User = Name
def Get_ID(self):
return Number
class Telephone(User):
def __init__(self,Number,Name,TelephoneNumber):
User.__init__(self,Number,Name)
Telephone = TelephoneNumber
def Get_Telephone(self):
return Telephone
TheArray = []
Phone = Telephone(1, "Marc", 8888)
TheArray.append(Telephone)
Phone = Telephone(1, "Jack", 9999)
TheArray.append(Telephone)
答案 0 :(得分:1)
您可以使用__dict__
来获取对象的属性。如下所示:
counter = 0
TheArray = []
while Counter < 18:
VehicleClass = Vehicle("Red Sports Car", "RSC13", 15.00, Counter, "Car", 3.3, 12.1, 0.08)
TheArray.append(VehicleClass)
Counter = Counter + 1
print(TheArray)
for obj in TheArray:
print(obj.__dict__)
希望它能帮到你.. !!