定义一个汽车类类中定义一个move方法并添加颜色、马力、型号等属性分别创建BMW_X7、AUDI_A8对象分别打印出属性值、调用move方法
class Car:
def __init__(self, color, horsepower, model):
self.color = color
self.horsepower = horsepower
self.model = model
def move(self):
print("The car is moving.")
BMW_X7 = Car("Black", 300, "BMW X7")
AUDI_A8 = Car("White", 350, "AUDI A8")
print("BMW X7 - Color:", BMW_X7.color)
print("BMW X7 - Horsepower:", BMW_X7.horsepower)
print("BMW X7 - Model:", BMW_X7.model)
print("AUDI A8 - Color:", AUDI_A8.color)
print("AUDI A8 - Horsepower:", AUDI_A8.horsepower)
print("AUDI A8 - Model:", AUDI_A8.model)
BMW_X7.move()
AUDI_A8.move()
输出结果:
BMW X7 - Color: Black
BMW X7 - Horsepower: 300
BMW X7 - Model: BMW X7
AUDI A8 - Color: White
AUDI A8 - Horsepower: 350
AUDI A8 - Model: AUDI A8
The car is moving.
The car is moving.
``
原文地址: https://www.cveoy.top/t/topic/h6lS 著作权归作者所有。请勿转载和采集!