目录
1.has a
# has a:一个类中使用了另外一种自定义的类型(如Car中使用Road类) # 自定义类型:自定义的类 # 系统类型:str,int,float.tuple,set,dict,list.......
import random
class Road:
def __init__(self,name,len):
self.name = name
self.len = len
class Car:
def __init__(self,brand,speed):
self.brand = brand
self.speed = speed
def get_time(self,road): # road是一个类对象
ran_time = random.randint(1,10)
msg = '{}品牌的车在{}上以{}速度行驶{}小时'.format(self.brand,road.name,self.speed,ran_time)
print(msg)
def __str__(self):
return '品牌:{},速度:{}'.format(self.brand,self.speed)
r = Road('御澜高速',15000)
car = Car('宝马',130)
print(car)
print('--------------')
car.get_time(r)
# 品牌:宝马,