1.编写一个表示汽车的类Car,定义__init__()方法初始化对象的属性:品牌、里程数、外观颜色、 。将里程属性值设置为私有属性。根据课堂讲解,定义汽车类的两个功能方法:汽车的行驶功能run()方法、汽车的载客功能overload()方法。并增加两个方法get_mileage()、set_mileage(),其中get_mileage()方法用于获取车辆的行驶里程,set_mileage()方法用于修改车辆的行驶里程。用Car类来创建某辆特定的汽车对象my_new_car,调用set_mieage()方法,设置里程数,调用get_mieage()方法
class car():
def __init__(self,brand,mileage,color,seats) -> None:
self.brand=brand
self.mileage=mileage
self.color=color
self.seats=seats
def run(self):
return f'牌子是:{self.brand},能行驶公里:{self.mileage},颜色:{self.color},座位:{self.seats}'
def overload(self,num_passengers):
print(f'核载人数:{self.seats}人')
if num_passengers>self.seats:
return '超载'
else:
print(f'w未超载,人数为:{num_passengers}')
def get_mileage(self):
return f'历程为{self.mileage}'
def set_mileage(self,new_mileage):
self.mileage=new_mileage
def discribe_car(self):
print(f'牌子是:{self.brand},能行驶公里:{self.mileage},颜色:{self.color},座位:{self.seats}')
newcar=car('bens',2,'黑色',5)
print(newcar.run())
print(newcar.overload(3))
2.
在题目1定义汽车Car类的基础上创建电动汽车类ElectricCar,其中Car类中包括初始化属性、描述车辆基本信息的discribe_car()方法、获取车辆的行驶里程get_mileage()方法、修改车辆的行驶里程set_mieage()方法、汽车的行驶功能run()方法、汽车的载客功能overload()方法。
根据电动汽车类ElectricCar创建子类对象my_byd,并调用从父类Car中继承的discribe_car()方法。
class elecar(car):
def __init__(self, brand, mileage, color, seats,battery_capacity,endurance) -> None:
super().__init__(brand, mileage, color, seats)
self.battery_capacity=battery_capacity
self.endurance=endurance
def discribe_battery(self):
print(f'电池容量为:{self.battery_capacity},续航里程:{self.endurance}')
new_car=elecar('byd',8,'白',6,500,100)
print(new_car.discribe_car())
print(new_car.discribe_battery())
3.
创建“People”类,定义__init__()方法初始化对象的属性:姓名、性别、电话号码。定义“People”类的两个功能方法(打印输出姓名、性别、电话号码信息print_infor()方法;更新电话号码update_phone()方法)。并由该类生成具体的对象“Tom”,打印他的电话信息、更新他的电话号码。
class people():
def __init__(self,name,sex,phone) -> None:
self.name=name
self.sex=sex
self.phone=phone
def info(self):
print(f'名字是:{self.name},性别:{self.sex},电话:{self.phone}')
def update_phone(self,newphone):
self.phone=newphone
tims=people('tims','male',1231321)
print(tims.info())
print(tims.update_phone(77777))
print(tims.info())
在“Teacher”类中添加Teacher类拥有独特的属性:office办公室;重写(覆盖)继承自父类的print_infor()方法,打印教师信息; 添加调换(更新)教师办公室的方法:update_office()。
class teacher(people):
def __init__(self, name, sex, phone,office) -> None:
super().__init__(name, sex, phone)
self.office=office
def pri_info(self):
print(f'老师姓名:{self.name},性别:{self.sex},电话:{self.phone},办公室:{self.office}')
def update_office(self,new_office):
self.office=new_office
mko=teacher('mko','male',189237,'三楼')
print(mko.pri_info())
print(mko.update_office('四楼'))
print(mko.pri_info())