把一个类的实例作为一个属性使用该怎么传参
大概有两个问题:1.怎么传参,2怎么调用作为属性的实例的方法
{:7_119:}
class allcar():
"""定义一个所有车车都有的属性,并将其实例用于其他车车的一个属性"""
def __init__(self,colour,sign):
"""定义车车的颜色,牌子,初始化行车里程"""
self.colour = colour
self.sign = sign
self.long = 0
def add(self,your_long):
"""添加此次出行的里程记录"""
if your_long < 0 :
print("there is an error in your data!!")
else :
self.long += your_long
class car():
"""一个小汽车"""
def __init__(self,colour,sign,oil=100):
"""定义车车的颜色,牌子,初始化行车里程"""
self.sx = allcar(colour,sign)
def sy(self,your_long):
"""计算剩余油量"""
this_oil = your_long * 2
self.oil -= this_oil
class dd_car():
"""这是一个电动的汽车"""
def __init__(self,colour,sign,self.dl = 100):
self.sx = allcar(colour,sign)
def sy(self,your_long):
"""计算剩余电量"""
this_dl = your_long * 3
self.dl -= this_dl
my_car = car('黄色','宝马')
luc = int(input("此次行程共行驶了(公里):"))
my_car.sx.add(luc)
my_car.sy(luc)
print("我的车是" + my_car.colour +'的' + my_car.sign)
print("该车共计行驶了" + str(my_car.long) + "公里")
print("该车剩余油量为:" + str(my_car.oil))
your_car = dd_car('蓝色','美的')
luc = int(input("此次行程共行驶了(公里):"))
your_car.sx.add(luc)
your_car.sy(luc)
print("你的车是" + your_car.colour +'的' + your_car.sign)
print("该车共计行驶了" + str(your_car.long) + "公里")
print("该车剩余电量为:" + str(your_car.dl))
你需要往下学习继承
页:
[1]