7.1 面向对象的基本概念

7.2 类
类:对具有相同属性和方法的一组对象的描述和定义

class Car(): #创建一个类,类的名称为Car。类的名称一般首字母大写
"""汽车目前价值估计程序""" #对该类进行文字说明,让读者可以快速获知该类的作用
def __init__(self,make,model,year): #下划线__是一种约定,避免Python默认的方法与普通的方法名称冲突
#self必不可少,且必须位于其他形参的前面
self.make=make #可以通过对象访问的变量称为属性
self.model=model
self.year=year
def detection(self): #方法detection
duration=2018-self.year
price=30-2*duration
long_name="你的"+self.make+self.model\
+"到目前已经行驶了"+str(duration)\
+"年,"+"目前价值"+str(price)+"万"
return long_name
my_car=Car("Audi","A4",2013)
print(my_car.year)
result=my_car.detection()
print(result)
运行结果如下:

7.2.2 属性
给属性指定的默认值
self.属性名=默认值
修改属性的值:
对象名称.属性名=修改值
class Car(): #创建一个类,类的名称为Car。类的名称一般首字母大写
"""汽车目前价值估计程序""" #对该类进行文字说明,让读者可以快速获知该类的作用
def __init__(self,make,model,year): #下划线__是一种约定,避免Python默认的方法与普通的方法名称冲突
#self必不可少,且必须位于其他形参的前面
self.make=make #可以通过对象访问的变量称为属性
self.model=model
self.year=year
self.this_year=2018 #给属性一个默认值
def detection(self): #方法detection
duration=self.this_year-self.year
price=30-2*duration
long_name="你的"+self.make + self.model\
+"到目前已经行驶了"+str(duration)\
+"年,"+"目前价值"+str(price)+"万"
return long_name
my_car=Car("Audi","A4",2013)
my_car.this_year=2021 #修改属性的值,无法通过传参改变默认值,只能进行赋新值
print(my_car.year)
result=my_car.detection()
print(result)
class Car(): #创建一个类,类的名称为Car。类的名称一般首字母大写
"""汽车目前价值估计程序""" #对该类进行文字说明,让读者可以快速获知该类的作用
def __init__(self,make,model,year): #下划线__是一种约定,避免Python默认的方法与普通的方法名称冲突
#self必不可少,且必须位于其他形参的前面
self.make=make #可以通过对象访问的变量称为属性
self.model=model
self.year=year
self.this_year=2018 #给属性一个默认值
def mod_this_year(self,new_year): #方法二 修改属性新的年份,通过建立新的函数
self.this_year=new_year
def detection(self): #方法detection
duration=self.this_year-self.year
price=30-2*duration
long_name="你的"+self.make + self.model\
+"到目前已经行驶了"+str(duration)\
+"年,"+"目前价值"+str(price)+"万"
return long_name
my_car=Car("Audi","A4",2013)
my_car.mod_this_year(2021) #方法二:修改属性的值
print(my_car.year)
result=my_car.detection()
print(result)
运行结果如下:

7.3 继承
当编写的类是另一个已经存在的类的特殊版本,可使用继承。
一个类继承另一个类时,他将自动获得另一个类的所有属性和方法
原来的类称为父类,新的类称为子类。

规则
1、采用子类创建的对象,可以使用子类的方法也可以使用父类的方法。使用父类创建的对象,只可以使用父类的方法。
2、当子类中的方法与父类中的方法同名时,称作重写父类,此时使用子类创建的对象,会直接使用子类创建的同名方法。但是父类创建的对象,仍然使用父类中的同名方法。
3、子类的创建必须在父类创建的后面
4、在子类中定义的方法,只能通过在子类中创建的对象中使用该方法。不能通过父类创建的对象中使用
class Car(): #创建一个类,类的名称为Car。类的名称一般首字母大写
"""汽车目前价值估计程序""" #对该类进行文字说明,让读者可以快速获知该类的作用
def __init__(self,make,model,year): #下划线__是一种约定,避免Python默认的方法与普通的方法名称冲突
#self必不可少,且必须位于其他形参的前面
self.make=make #可以通过对象访问的变量称为属性
self.model=model
self.year=year
self.this_year=2018 #给属性一个默认值
def mod_this_year(self,new_year): #方法二修改新的年份
self.this_year=new_year
def detection(self): #方法detection
duration=self.this_year-self.year
price=30-2*duration
long_name="你的"+self.make + self.model\
+"到目前已经行驶了"+str(duration)\
+"年,"+"目前价值"+str(price)+"万"
return long_name
class ElectricCar(Car): #父类必须位于子类的前面
def __init__(self,make,mode,year): #super函数将父类和子类关联起来。让Python调用ElectricCar的父类的方法__init__()
super().__init__(make,mode,year) #父类称为超类,super因此得名。
def battery(self,capacity):
self.capacity_num=capacity
print("您选的电池容量是:",self.capacity_num,"kwh")
def detection(self): #重写父类 #在子类创建的方法如果同父类相同,通过子类创建的对象会使用子类的该方法。
duration=self.this_year-self.year #通过父类创建的对象会使用父类的该方法。
price=30-duration-(500/self.capacity_num)
long_name="你的"+self.make+self.model+"到目前为止已经行驶了"+\
str(duration)+"年,"+"目前价值是:"+str(price)+"万"
return long_name
my_Tesla =ElectricCar("Tesal","model",2017)
print(my_Tesla.year)
my_Tesla.battery(100)
result=my_Tesla.detection()
print(result)
运行结果如下:

7.4 导入类
Python可以将类存储在模块中,然后在主程序中导入所需的模块。
1、导入单个类:

2、从一个模块中导入多个类:

导入单个类:
1、新建一个文件

2、父类写到文件中car_file中

3、子类写到文件car_file中

从模块中一同导入多个类
from car_file import Car,ElectricCar
my_Tesla =ElectricCar("Tesal","model",2017)
print(my_Tesla.year)
my_Tesla.battery(100)
result=my_Tesla.detection()
print(result)
3、导入整个模块


import car_file
my_car=car_file.Car("Audi","A4",2013)
my_car.mod_this_year(2014)
print(my_car.this_year)
result=my_car.detection()
print(result)
my_Tesla =car_file.ElectricCar("Tesal","model",2017)
print(my_Tesla.year)
my_Tesla.battery(100)
result=my_Tesla.detection()
print(result)
4、导入模块中的所有类

from car_file import * #导入了所有类
my_car=Car("Audi","A4",2013)
my_car.mod_this_year(2014)
print(my_car.this_year)
result=my_car.detection()
print(result)
my_Tesla =ElectricCar("Tesal","model",2017)
print(my_Tesla.year)
my_Tesla.battery(100)
result=my_Tesla.detection()
print(result)
这种方法虽然简单,但存在一定弊端,有可能因为内存导致程序不稳定。
本文深入讲解面向对象编程的核心概念,包括类的定义、属性、方法、继承及类的实例化。探讨如何通过类创建对象,设定属性的默认值,以及如何修改属性值。同时,介绍了子类继承父类的机制,以及如何在子类中重写父类的方法。最后,展示了不同方式导入类的方法,以便在实际项目中灵活运用。
11万+

被折叠的 条评论
为什么被折叠?



