目录
实验目的:
- 掌握类的定义和使用。
- 掌握类和对象的关系、类的属性和方法;
- 熟练设计和使用类;
- 熟悉运算符的重载。
实验任务:
- 定义一个表示汽车的类 Car。它存储了有关汽车的信息,包括厂商make、型号 model、年份 year、以及里程 odometer(默认值为 0);还有汇总这些信息的方法,包括获取描述信息 get_descriptive_name()、获取汽车里程get_odometer()、以及更新里程 update_odometer()。创建一个新车的实例,打印汽车的描述信息,并读取初始里程数,修改里程数后再次读取当前里程数。运行结果类似下图:
- 基于上述 Car 类,创建一个表示电动汽车的 ElectricCar 类的,具备 Car 类的所有功能。在ElectricCar 类中添加电动汽车特有的属性(电瓶battery_size)以及一个描述该属性的方法 describe_battery()。创建一个新电动车的实例,打印汽车的描述信息,并打印有关电瓶的信息。运行结果类似下图:
- 自定义复数类,重载运算符加、减、乘或除。本实例要求编写代码,重载运算符,使列表支持四则运算。
实验步骤:
- 创建一个项目 python5,向其中添加一个 python 文件 python5_1.py。首先,定义一个表示汽车的类 Car,在构造方法 init ()中指定厂商 make、型号model、年份 year、以及里程 odometer(默认值为 0);然后,定义获取描述信息的方法 get_descriptive_name(),用于组织并返回汽车的 year、make、model 信息,具体格式请按任务 1 的样例设计;定义获取汽车里程的方法get_odometer(),打印汽车当前的里程数;定义更新里程的方法update_odometer(),用于重新给里程属性赋值;最后,创建一个新车的实例, 调用 get_descriptive_name()打印汽车的描述信息,并调用 get_odometer()方法读取初始里程数,调用 update_odometer()方法修改里程数后再次调用get_odometer()方法读取当前里程数。
- 在项目 python5 中新建文件 python5_2.py。首先,将 python5_1.py 中的Car 类复制到 python5_2.py 中;然后,创建一个继承 Car 类的电动汽车类ElectricCar,在 ElectricCar 类的构造函数 init ()中,使用 super()函数调用父类 Car 的构造函数 init (),将 Car 的 init() 方法中定义的所有属性包含到ElectricCar 类中;接着,在 ElectricCar 类的 init ()中添加电动汽车特有的属性 battery_size;在 ElectricCar 类中定义以及一个描述电池属性的方法describe_battery(),打印电池的信息;最后,创建一个新电动车的实例,调用get_descriptive_name()方法打印汽车的描述信息,并调用 describe_battery()打印有关电瓶的信息。
- 在项目 python5 中新建文件 python5_3.py。首先,定义一个表示复数的类 Complex;然后,在其中重载运算符加减乘除 add ()、 sub ()、 mul() 、 truediv() ;最后,创建两个复数类的实例,调用+、-、*、/来查看复数运算的结果。
参考代码:
#python5_1
class Car:
def __init__(self,make,model,year,odometer=0):
self.make=make
self.model=model
self.year=year
self.odometer=odometer
def get_descriptive_name(self):
return str(self.year)+' '+self.make+' '+self.model
def get_odometer(self):
print('This car has',self.odometer,'mile on it')
def update_odometer(self,newodo):
self.odometer=newodo
c=Car('Toyota','Rav4',2021)
print(c.get_descriptive_name())
c.get_odometer()
c.update_odometer(23)
c.get_odometer()
#python5_2
class Car:
def __init__(self,make,model,year,odometer=0):
self.make=make
self.model=model
self.year=year
self.odometer=odometer
def get_descriptive_name(self):
return str(self.year)+' '+self.make+' '+self.model
def get_odometer(self):
print('This car has',self.odometer,'mile on it')
def update_odometer(self,newodo):
self.odometer=newodo
class ElectricCar(Car):
def __init__(self,make,model,year,battery_size,odometer=0):
super().__init__(make,model,year,odometer=0)
self.battery_size=battery_size
def describe_battery(self):
print("This car has a",self.battery_size,"battery")
ec=ElectricCar('Tsela','Model S',2019,'75-kwh')
print(ec.get_descriptive_name())
ec.describe_battery()
#python5_3
class Complex:
def __init__(self,data):
if isinstance(data, str):
l=data.split('+')
self.real = float(l[0])
self.imag = float(l[1].strip('j'))
def __add__(self,other):
if isinstance(other, Complex):
a=self.real
b=self.imag
c=other.real
d=other.imag
x=a+c
y=b+d
if y==0:
return x
if y>0:
return str(x)+'+'+str(y)+'j'
else:
return str(x)+str(y)+'j'
def __sub__(self,other):
if isinstance(other, Complex):
a = self.real
b = self.imag
c = other.real
d = other.imag
x = a-c
y = b-d
if y==0:#虚部为0,直接输出实部
return x
if y > 0:
return str(x) + '+' + str(y) + 'j'
else:
return str(x) + str(y) + 'j'
def __mul__(self,other):
if isinstance(other, Complex):
a = self.real
b = self.imag
c = other.real
d = other.imag
x = a*c-b*d
y = a*d+b*c
if y==0:
return x
if y > 0:
return str(x) + '+' + str(y) + 'j'
else:
return str(x) + str(y) + 'j'
def __truediv__(self,other):
if isinstance(other, Complex):
a = self.real
b = self.imag
c = other.real
d = other.imag
k=c**2+d**2
e = a * c+b * d
f = b*c-a*d
x=e/k
y=f/k
if y==0:
return x
if y > 0:
return str(x) + '+' + str(y) + 'j'
else:
return str(x) + str(y) + 'j'
c1=Complex("6+2j")
c2=Complex("3+2j")
print(c1+c2)
print(c1-c2)
print(c1*c2)
print(c1/c2)