Python类
代码:
#!/usr/local/bin/python3
"""Class Car and Class ElectricCar"""
class Car():
"""class Car includes 'Maker', 'Model', 'Year' and ‘Color’"""
def __init__(self, Maker, Model, Year):
self.Maker = Maker
self.Model = Model
self.Year = Year
def SetColor(self, Color):
self.Color = Color
def GetLongName(self):
return self.Maker + ' ' + self.Model + ' ' + str(self.Year)
def GetInfo(self):
return self.Maker, self.Model, self.Year, self.Color
if __name__ == '__main__':
# execute only if run as a script
def main():
mycar = Car('Audi', 'A6L', '2018')
print()
print(mycar.GetLongName())
mycar.SetColor('Black')
print(f'The color is %s.' %mycar.Color)
main()
输出结果:
Audi A6L 2018
The color is Black.