Python入门到实践(9)-类的继承
2017-12-04 22:11
267 查看
# -*- coding: utf-8 -*- """ Created on Mon Dec 04 21:22:37 2017 @author: Echo """ #类的继承 class Car():#父类 """一次模拟汽车的尝试""" def __init__(self,make,model,year): self.make=make self.model=model self.year=year self.odometer_reading=0 #制定属性的默认值 def get_descriptive_name(self): long_name=str(self.year)+' '+self.make+' '+self.model return long_name.title() def read_odmeter(self): print("This car has "+str(self.odometer_reading)+"miles on it!") def update_odmeter(self,mileage):#防止私自修改历程数属性 if mileage>=self.odometer_reading: self.odometer_reading-mileage else: print("请输入大于当前里程的数值") def fill_gas_tank(): print("This Car dosen't have gas") class ElectriCar(Car): #子类 def __init__(self,make,model,year): Car.__init__(self,make,model,year) self.battery=70#添加子类属性 def discribe_barrery(self):#添加子类方法 print("This car has "+str(self.battery)+"- KWh battery!") def fill_gas_tank():#重写父类方法 print("This Car dosen't need a gas tank!") my_electricar=ElectriCar('tesla','A3',2015) print(my_electricar.get_descriptive_name()) class Battery():#电池类 def __init__(self,battery_size=70): self.battery_size=battery_size def describe_battery(): print("This car has "+str(self.battery)+"- KWh battery!") class ElectriCarNew(Car): #子类 def __init__(self,make,model,year): Car.__init__(self,make,model,year) self.battery=Battery()#使用实例作为其他类的属性
相关文章推荐
- python入门第三天——继承
- python敲得代码(从入门到实践书中代码)
- python从入门到实践第七章的练习题作业
- python编程 从入门到实践 第八章 函数 及课后题
- Python编程 从入门到实践 第四章 作业参考答案 (部分)
- Python数据挖掘入门与实践(一)——亲和性分析
- 《python数据挖掘入门与实践》决策树预测nba数据集
- 《python数据挖掘入门与实践》“电影推荐” 笔记3
- Python入门:类与类的继承
- 学习笔记_python_python编程从入门到实践3.0
- 9-5(python编程从入门到实践)
- Python Web入门:Django学习与实践之三(models)
- python编程从入门到实践18章 Django入门
- python从入门到实践 第八章习题(高级编程技术 week4-2)
- python从入门到实践 第九章习题(高级编程技术 week5-1)
- Learning Data Mining with Python-《Python数据挖掘入门与实践》学习后的分享
- python从入门到实践第三章习题(高级编程技术 week2-1)
- Python编程 从入门到实践 第三章 作业参考答案
- 《Python 编程-从入门到实践》7-1~7-10
- Python网页抓取与爬虫基本实践-入门篇