码迷,mamicode.com
首页 > 其他好文 > 详细

电动汽车模块

时间:2021-02-19 13:19:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:类的属性   style   bin   import   ram   desc   char   mes   描述   

cat  electric_car.py 

 1 #! /usr/bin/python
 2 # -*- coding:utf-8 -*-
 3 
 4 from car import Car
 5 
 6 class Battery(object):
 7     """
 8     模拟电瓶
 9     """
10     def __init__(self, battery_size=70):
11         """初始化电瓶的属性"""
12         self.battery_size = battery_size
13 
14     def describe_battery(self):
15         """描述电瓶容量的消息"""
16         print "This car has a " + str(self.battery_size) + "-kwh battery."
17 
18     def get_range(self):
19         """
20         打印描述电瓶续航里程的消息
21         :return:
22         """
23         if self.battery_size == 70:
24             range1 = 240
25         elif self.battery_size == 85:
26             range1 = 270
27 
28         message = "This car can go approximately " + str(range1)
29         message += " miles on a full charge."
30         print message
31 
32 
33 class ElectricCar(Car):
34     """电动汽车独特之处"""
35     def __init__(self, make, model, year):
36         """
37         初始化父类的属性,再初始化电动汽车特有的属性
38         :param make:
39         :param model:
40         :param year:
41         """
42         super(ElectricCar, self).__init__(make, model, year)  # 继承
43         self.battery = Battery()  # 将实例用作属性
44 
45     def full_gas_tank(self):  # 重写父类的方法
46         """电动车不需要邮箱"""
47         print "This car doesn‘t need a gas tank!"

 

电动汽车模块

标签:类的属性   style   bin   import   ram   desc   char   mes   描述   

原文地址:https://www.cnblogs.com/leejay/p/14409729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!