有无数种方法来“解决”这个问题,但我会介绍几种。
没有“正确”的方法,所有方法都涉及权衡,在一段时间内,您将学会在哪些情况下使用哪种方法。
传递参数
class Vehicle():
"""To model a basic vehicle"""
def __init__(self, name, max_speed, millage, tyre_size, tyre_pressure):
"""Initiate class variables"""
self.name = name
self.max_speed = max_speed
self.millage = millage
self.tyre = Tyre(tyre_size, tyre_pressure)
def vehicle_details(self):
"""display vehicle details"""
return f"\nName: {self.name.title()}, Max speed: {self.max_speed}MPH, Millage: {self.millage} "
class Tyre():
"""A class specific for vehicle tyres"""
def __init__(self, size, pressure):
self.size = size
self.pressure = pressure
def tyre_details(self):
"""Print tyre details."""
print(f'Tyre size: {self.size} inches.')
print(f"Tyre pressure {self.pressure} PSI.")
v1 = Vehicle("VehicleName", 120, 50_000, 16, 36)
v2 = Vehicle(
name = "VehicleName",
max_speed = 120,
millage = 50_000,
tyre_size = 16,
typre_pressure = 36
)
在这里,你只是从Vehicle
构造函数将参数传递给了Tyre
的构造函数来构建Tyre
。如果每次构造一个Vehicle
时都知道Tyre
的规格,那么这样做就没问题。如果不知道,你可能需要另一种方法。
使用Tyre
实例作为参数构造Vehicle
class Vehicle():
"""To model a basic vehicle"""
def __init__(self, name, max_speed, millage, tyre):
"""Initiate class variables"""
self.name = name
self.max_speed = max_speed
self.millage = millage
self.tyre = tyre
def vehicle_details(self):
"""display vehicle details"""
return f"\nName: {self.name.title()}, Max speed: {self.max_speed}MPH, Millage: {self.millage} "
class Tyre():
"""A class specific for vehicle tyres"""
def __init__(self, size, pressure):
self.size = size
self.pressure = pressure
def tyre_details(self):
"""Print tyre details."""
print(f'Tyre size: {self.size} inches.')
print(f"Tyre pressure {self.pressure} PSI.")
v = Vehicle(
name = "VehicleName",
max_speed = 120,
millage = 50_000,
tyre = Tyre(16, 36)
)
与前一个选项类似,但在前一个选项中,每个
Vehicle
都有自己的
Tyre
实例。 在这里,您可以“共享”
Tyre
对象。 在这种情况下可能不是很重要,但是需要牢记,特别是当您使用更大或更复杂的对象并/或共享成为一个好处时。
共享
Tyre
实例可能如下所示:
standard_tyre = Tyre(16, 36)
v1 = Vehicle(
name = "VehicleOne",
max_speed = 120,
millage = 50_000,
tyre = standard_tyre,
)
v2 = Vehicle(
name = "VehicleTwo",
max_speed = 140,
millage = 20_000,
tyre = standard_tyre,
)
在构造函数之外设置轮胎
class Vehicle():
"""To model a basic vehicle"""
def __init__(self, name, max_speed, millage):
"""Initiate class variables"""
self.name = name
self.max_speed = max_speed
self.millage = millage
self.tyre = None
def vehicle_details(self):
"""display vehicle details"""
return f"\nName: {self.name.title()}, Max speed: {self.max_speed}MPH, Millage: {self.millage} "
class Tyre():
"""A class specific for vehicle tyres"""
def __init__(self, size, pressure):
self.size = size
self.pressure = pressure
def tyre_details(self):
"""Print tyre details."""
print(f'Tyre size: {self.size} inches.')
print(f"Tyre pressure {self.pressure} PSI.")
v = Vehicle(
name = "VehicleName",
max_speed = 120,
millage = 50_000,
)
v.tyre = Tyre(16,36)
也许在创建 "Vehicle" 时,您不知道 "Tyre" 的具体细节,但仍然想要一个 "Vehicle" 实例。
size
和pressure
参数添加到车辆类构造函数中,如:def __init__(self, name, max_speed, millage, tyre_size, tyre_pressure)
,并在创建时将它们传递给轮胎:self.tyre = Tyre(tyre_size,tyre_pressure)
。 - Abhigyan