class Peason():
def __init__(self, name):
self.name = name
def go_to(self, place, vehice):
self.place = place
self.vehice = vehice
self.vehice.run()
print(f"{self.name}驾驶{self.vehice.brand}{self.vehice}去{self.place}")
class Car():
def __init__(self, brand):
self.brand = brand
def run(self):
print("行驶")
lz = Peason("老张")
car = Car("奔驰")
lz.go_to("东北", car)
行驶
老张驾驶奔驰<__main__.Car object at 0x7f8380dcdd90>去东北
重写Car类的__str__方法
class Car():
def __init__(self, brand):
self.brand = brand
def run(self):
print("行驶")
def __str__(self):
return self.brand
self.vehice不对,这获取的是car呀
self.vehice.brand不已经是奔驰了吗,后面还添加参数你到底想输出什么呀