Python 类与对象示例:创建汽车类
class Car: def init(self, make, model, year, color): self.make = make self.model = model self.year = year self.color = color
def start(self):
print(f'{self.color} {self.make} {self.model} is starting.')
def drive(self):
print(f'{self.color} {self.make} {self.model} is driving.')
def stop(self):
print(f'{self.color} {self.make} {self.model} is stopping.')
my_car = Car('Toyota', 'Corolla', 2021, 'black') my_car.start() my_car.drive() my_car.stop()
原文地址: http://www.cveoy.top/t/topic/lYe6 著作权归作者所有。请勿转载和采集!