时间:2022年1月4日20:35:35
感受:
- 对于多态的操作还需要再进行深度的理解
- 还是要更多的动手实操
题目
- 1、使用python,选择小汽车和客车为对象,使用类实现它们的属性和方法(汽车的重量属性,驾驶的方法),并打印属性
- 2、对第一个问题改写:继承客车的属性和方法,改造为公交车,输入的名字为:“东风路28路公交车”,并打印(也包括属性和驾驶的方法)
- 3、使用多态实现28路、906路、B32路公交车的驾驶方法并打印

主函数
from first import qiche
from first import keche
from second import bus
from third import *
def first():
x = qiche()
y = keche()
print(x.wight)
print(x.cdrive())
print(y.wight)
print(y.cdrive())
def second():
x = bus()
print(x.wight)
x.cdrive()
def third():
x = b28()
y = b906()
z = b32()
func(x)
func(y)
func(z)
if __name__ == '__main__':
print("*******************************")
first()
print("*******************************")
second()
print("*******************************")
third()
第一题
class qiche():
wight = '1t'
def __init__(self):
self.drive = self
self.drive = 'C证'
def cdrive(self):
return self.drive
class keche():
wight = '2t'
def __init__(self):
self.drive = self
def cdrive(self):
self.drive = 'A证'
return self.drive
第二题
from first import keche
class bus(keche):
wight = '1.5t'
def __init__(self, drive = '', name = ''):
keche.__init__(self)
self.name = name
self.drive = drive
self.drive = 'A证'
self.name = '东风路28路公交车'
def cdrive(self):
print(self.name, self.drive)
第三题
class car():
def who(self):
print("I am driving car")
class b28(car):
def who(self):
print("I am driving N0.28 bus")
class b906(car):
def who(self):
print("I am driving N0.906 bus")
class b32(car):
def who(self):
print("I am driving N0.B32 bus")
def func(obj):
obj.who()