类的操作-汽车类

#author = ‘zhanghang‘#coding :-*- utf-8 -*-

class Car():#定义car类    def __init__(self,make,model,year):        "初始化描述汽车的属性"        self.make = make        self.model = model        self.year = year        self.odometer_reading = 0 #设置里程属性初始值为0 ,无需在init中设置形参    def get_describe_name(self):        "返回整洁的汽车描述信息"        long_name = str(self.year) + ‘ ‘ + self.make + ‘ ‘ + self.model        return long_name.title()    def update_odometers(self,miles):        "通过方法修改属性odometer_reading的值"        self.odometer_reading += miles    def reading_odometer(self):        "读取汽车的里程"        print("This car has " + str(self.odometer_reading) + " miles on it.")

class Battery():    "电瓶具有自己的属性和方法,单独做一个类"    def __init__(self,battery_size):        self.battery_size = battery_size    def upgrade_battery(self):        if self.battery_size<85:            self.battery_size = 85    def describe_battery(self):        print("This EletricCar‘s battery is " + str(self.battery_size) + "-kwh")    def get_range(self):        if self.battery_size <= 70:            range = 240        else:            range = 280        message = "This car can go approximately " + str(range)        message += " miles on a full charge"        print(message)

class ElectricCar(Car):#电动汽车继承汽车类    "子类ElectricCar继承父类Car"    def __init__(self,make,model,year):        super().__init__(make,model,year) #初始化父类的属性        self.battery = Battery(70) #实例用作属性    def get_describe_name(self):        "假设子类ElectricCar与父类Car的描述不一致,则在子类中重新定义同名方法,Python将不考虑父类中该方法"        print("The ElectricCar‘s type is " + str(self.year) + ‘ ‘ + self.make + ‘ ‘ + self.model)

my_car = Car(‘audi‘,‘a4‘,2016)#根据类创建实例print(my_car.get_describe_name())my_car.update_odometers(2000)my_car.reading_odometer()

my_eletricCar = ElectricCar(‘aima‘,‘e1‘,2017)#根据子类创建实例my_eletricCar.get_describe_name()my_eletricCar.update_odometers(2000)my_eletricCar.reading_odometer()my_eletricCar.battery.describe_battery()my_eletricCar.battery.get_range()my_eletricCar.battery.upgrade_battery() #通过类Battery()里的upgrade_battery将battery更改为85kwhmy_eletricCar.battery.describe_battery() #my_eletricCar.battery传值到类Battery() 调用Battery()的方法describe_batterymy_eletricCar.battery.get_range()

运行结果:2016 Audi A4This car has 2000 miles on it.The ElectricCar‘s type is 2017 aima e1This car has 2000 miles on it.This EletricCar‘s battery is 70-kwhThis car can go approximately 240 miles on a full chargeThis EletricCar‘s battery is 85-kwhThis car can go approximately 280 miles on a full charge
时间: 2024-10-20 08:16:53

类的操作-汽车类的相关文章

C#修改MAC地址类及操作网卡类

1.更新MAC地址 将注册表中的键值添加上MAC地址 2.重新连接网络 试过了3个方法: ManagementClass最新提供了Disable,Enable方法,但只支持Vista操作系统 Shell.dll的方法,可以实现,但处理起来很烦,另外在重新连接时显示"启动中"提示框,不友好. NetSharingManagerClass 的Disconnect, Connect方法,可以实现,但有一个问题是,会重新更新IP地址,有明显感觉等. using System; using Sy

【C#公共帮助类】JsonHelper 操作帮助类, 以后再也不用满地找Json了,拿来直接用

 四个主要操作类:JsonConverter .JsonHelper .JsonSplit .AjaxResult 一.JsonConverter: 自定义查询对象转换动态类.object动态类转换json包.json转换object动态类.DataReader转换为Json.DataSet转换为Json.DataTable转成Json.Datatable转换为Json .格式化字符型日期型布尔型.过滤特殊字符等 using System; using System.Collections.Ge

C++的继承操作---基类指针访问派生类问题---基类成员恢复访问属性问题

#include "stdafx.h" #include <iostream> #include <algorithm> using namespace std; class Base { public: int num; virtual void func() { cout<<"Do something in Base"<<endl; } }; class Derived:private Base { public:

Asp.Net 文件操作基类

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.T

C++String类关键操作简介

1.string 类的头文件 #include<string> using std::string; 2.string 类的构造函数 string s1; //默认的构造函数,s1为空串 string s2(s1); //将s2初始化为s1的一个副本,对s2的操作不会影响s1 string s3("value"); //将s3初始化为一个字符串字面值的副本 string s4(n,'c'); //将s4初始化为字符c的n个副本 3.string 对象的读写 cin >

java vm (二) 之 虚拟机启动对类的操作

虚拟机在启动中对类的操作 虚拟机加载类(classloader克拉斯楼的),类进行处理以后是字节码,虚拟机认识就可以做下面的事情了.从硬盘到内存的过程 链接 对类进行验证,类的头部用两个字节保存了,这个信息,详情找个专业的文档链接放到这里. 为类分配内存,给对应的变量附言默认值 int 给他 个0  Integer 给他个null…… 将特殊的字符进行解析,应该是跟Spring 对 @autowrite 的解析类似,有自己的套路,(找个合适的例子放到这里) 初始化变量赋予正确的值 比热 priv

perties类的操作

http://www.cnblogs.com/bakari/p/3562244.html perties类的操作 知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用Java来写,外加一些脚本语言Python,Perl之类的,不得已,又得重新拾起.本文通过看<Java编程思想>和一些网友的博客总结而来,只为简单介绍Properties类的相关操作. 一.Java P

JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/

/** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] args) { String[] s1 = {"a","b","c","d","e"}; String[] s2 = {"a","b","c",&qu

一个对缓存操作的类DataCache

using System;using System.Web;using System.Collections; namespace eBlog.Cache{ /// <summary> /// 缓存相关的操作类 /// </summary> public class DataCache { /// <summary> /// 获取当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="c