类名一般大写(驼峰命名法),类中包括属性与方法,
实例和方法一般小写+下划线
class Car:
def __init__(self,make,year):
self.make=make
self.year=year
def get_descriptive(self):
name=f"{self.make} {self.year}"
return name.title()
my_car = Car('q','2024')
print(my_car.get_descriptive())
f—string语法
print(f"Name is {表达式},year is {1+2}")
name.title()字符串首字母大写
类的继承,ElectricCar
包含父类于上方,可覆写父类方法
class ElecrticCar(Car):
def __init__(self, make, year):
super().__init__(make, year)
self.battery = 80
导入类
car.py包含所有类
from car import *
from car import Car
from car import Car , ElectricCar
可直接用
import car
car.Car调用
from car import ElectricCar as EC
指定别名