| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> 开发工具 -> python编程从入门到实践 笔记8 -> 正文阅读 |
|
[开发工具]python编程从入门到实践 笔记8 |
第九章?类1.创建和实用类①.创建Dog类class Dog(): #__init__()是一种特殊的方法,每当根据Dog类创建新实例时,Python都会自动运行它 #开头和结尾带有__是一种约束,旨在避免Python默认方法与普通方法发生冲突 def __init__(self,name,age): """初始化属性name和age""" self.name = name self.age = age def sit(self): """模拟小狗蹲下""" print(self.name.title() + " is now sitting! ") def roll_over(self): """模拟小狗打滚""" print(self.name.title()+" is rolling!") #根据类创建实例 my_dog = Dog('tom',6) # 访问属性 print("My dog is "+my_dog.name.title()) print("My dog is "+str(my_dog.age)+" years old!") # 调用方法 my_dog.sit() my_dog.roll_over() 运行结果如下: ?2.使用类和实例①.Car类# class Car(): # def __init__(self,make,model,year): # """初始化描述汽车的属性""" # self.make = make # self.model = model # self.year = year # # def get_descriptive_name(self): # long_name = str(self.year)+' '+self.make+' '+self.model # return long_name.title() # # my_new_car = Car('audi','a4',2016) # print(my_new_car.get_descriptive_name()) 运行结果: ? ②.给属性指定默认值# class Car(): # def __init__(self,make,model,year): # """初始化描述汽车的属性""" # self.make = make # self.model = model # self.year = str(year) # self.odometer_reading = 0 # # def get_descriptive_name(self): # long_name = self.year + " " + self.make + " " + self.model # return long_name.title() # # def read_odometer(self): # """打印一条指出汽车里程的消息""" # print("This car has "+ str(self.odometer_reading)+" miles on it") # # my_new_car = Car('audi','a4',2016) # print(my_new_car.get_descriptive_name()) # my_new_car.read_odometer() 运行结果: ?③.修改属性的值#方法1.直接修改属性的值 # class Car(): # def __init__(self,make,model,year): # """初始化描述汽车的属性""" # self.make = make # self.model = model # self.year = str(year) # self.odometer_reading = 0 # # def get_descriptive_name(self): # long_name = self.year + " " + self.make + " " + self.model # return long_name.title() # def read_odometer(self): # """打印一条指出汽车里程的消息""" # print("This car has "+ str(self.odometer_reading)+" miles on it") # # my_new_car = Car('audi','a4',2016) # print(my_new_car.get_descriptive_name()) # my_new_car.odometer_reading = 23 # my_new_car.read_odometer() 运行结果: ? #方法2.通过方法修改属性的值 # class Car(): # def __init__(self,make,model,year): # """初始化描述汽车的属性""" # self.make = make # self.model = model # self.year = str(year) # self.odometer_reading = 0 # # def get_descriptive_name(self): # long_name = self.year + " " + self.make + " " + self.model # return long_name.title() # # def read_odometer(self): # """打印一条指出汽车里程的消息""" # print("This car has "+ str(self.odometer_reading)+" miles on it") # # def update_odometer(self,mileage): # """将里程表读数设置为指定的值""" # self.odometer_reading = mileage # # my_new_car = Car('audi','a4',2016) # print(my_new_car.get_descriptive_name()) # # my_new_car.update_odometer(23) # my_new_car.read_odometer() 运行结果: ? #方法3.通过方法对属性的值进行递增 class Car(): def __init__(self,make,model,year): """初始化描述汽车的属性""" self.make = make self.model = model self.year = str(year) self.odometer_reading = 0 def get_descriptive_name(self): long_name = self.year + " " + self.make + " " + self.model return long_name.title() def read_odometer(self): """打印一条指出汽车里程的消息""" print("This car has "+ str(self.odometer_reading)+" miles on it") def update_odometer(self, mileage): """将里程表读数设置为指定的值""" if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): """将里程表读数增加指定的量""" self.odometer_reading += miles my_new_car = Car('audi','a4',2016) print(my_new_car.get_descriptive_name()) my_new_car.increment_odometer(12) my_new_car.update_odometer(10) my_new_car.read_odometer() 运行结果: ? 3.继承# 编写类时,并非总是要从空白开始。如果你要编写的类是另一个现成类的特殊版本,可使用继承 #一个类继承另一个类时,他将自动获得另一个类的所有属性和方法 #原有的类成为父类,新类为子类 #出了继承父类的属性和方法,还可以定义自己的属性和方法 1.子类的方法 __init__() #模拟电动汽车 #创建子类时,父类必须包含在当前文件中,且位于子类前面 #定义子类时,必须在括号内指定父类的名称 # 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_odometer(self): # """打印一条指出汽车里程的消息""" # print("This car has "+ str(self.odometer_reading)+" miles on it") # # def update_odometer(self, mileage): # if mileage >=self.odometer_reading: # self.odometer_reading = mileage # else: # print("You can't roll back an odometer!") # # def increment_odometer(self,miles): # self.odometer_reading += miles # class ElectricCar(Car): # """电动汽车的独特之处""" # def __init__(self,make,model,year): # """初始化父类的属性""" # super().__init__(make,model,year) # #super()帮助python将父类和子类关联起来 # 创建ElectricCar实例,赋给变量my_tesla # my_tesla = ElectricCar('tesla','models',2016) # print(my_tesla.get_descriptive_name()) 运行结果: ?2.给子类定义属性和方法 # class ElectricCar(Car): # """电动汽车的独特之处""" # def __init__(self,make,model,year): # """初始化父类的属性,再初始化电动汽车特有的属性""" # super().__init__(make,model,year) # self.battery_size = 70 # # def describe_battery(self): # """打印一条描述电瓶容量的消息""" # print("This car has a "+str(self.battery_size)+"-kwh battery.") # # my_tesla = ElectricCar('tesla','models',2016) # print(my_tesla.get_descriptive_name()) # my_tesla.describe_battery() 运行结果: ?3.重写父类的方法和将实例用作属性 #对于父类的方法,只要他不符合子类模拟的实物的行为,都可以对其进行重写 #重写完后,调用时会自动忽略父类的方法,只运行子类的方法 #当类的属性过多,可以提出来一部分单独列出来 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_odometer(self): """打印一条指出汽车里程的消息""" print("This car has "+ str(self.odometer_reading)+" miles on it") def update_odometer(self, mileage): if mileage >=self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): self.odometer_reading += miles class Battery(): """一次模拟电动汽车电瓶的简单尝试""" def __init__(self,battery_size=70): """初始化电瓶的属性""" self.battery_size = battery_size def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a "+str(self.battery_size)+"-kwh battery.") def get_range(self): """打印一条消息,指出电瓶的续航里程""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 else: range = 60 message = "This car can go approximately "+str(range) message += " miles on a full charge." print(message) class ElectricCar(Car): """电动汽车的独特之处""" def __init__(self,make,model,year): """初始化父类的属性,再初始化电动汽车特有的属性""" super().__init__(make,model,year) self.battery = Battery() my_tesla = ElectricCar('tesla','models',2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range() 运行结果: ? 4.导入类car.py """一个可用于表示汽车的类""" 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_odometer(self): """打印一条指出汽车里程的消息""" print("This car has "+ str(self.odometer_reading)+" miles on it") def update_odometer(self,mileage): """将里程表读数设置为指定的值,拒绝将里程表往回拨""" if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): """将里程表读数增加指定的量""" self.odometer_reading += miles class Battery(): """一次模拟电动汽车电瓶的简单尝试""" def __init__(self,battery_size=70): """初始化电瓶的属性""" self.battery_size = battery_size def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a "+str(self.battery_size)+"-bwh battery.") def get_range(self): """打印一条描述电瓶续航里程的消息""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately "+str(range) message += " miles on a full charge." print(message) class ElertricCar(Car): """模拟电动车的独特之处""" def __init__(self,make,model,year): """初始化父类的属性,再初始化电动汽车特有的属性""" super().__init__(make,model,year) self.battery = Battery() my_car.py from car import Car my_new_car = Car('audi','a4',2016) print(my_new_car.get_descriptive_name()) my_new_car.odometer_reading = 23 my_new_car.read_odometer() ? |
|
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 | -2024/11/16 1:55:31- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |