python

python(18):类(2)

2022-04-11  本文已影响0人  Z_bioinfo

1.继承

编写类时,并非总是要从空白开始,如果要编写的类时一个现成类的特殊版本,可使用继承
一个类继承另一个类时,将自动获得另一个类的所有属性和方法;原有的类称为父类,新类称为子类。
子类继承了父类的所有属性和方法,同时还可以定义自己的属性和方法

###1.子类的方法__init__()
#模拟电动汽车,电动汽车是一种特殊的汽车,因此可以在前面创建的Car()类基础上创建新的ElectricCar,这样就只需为电动汽车特有的属性和行为编写代码
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
    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 not roll back an odometer')
    def increment_odometer(self, miles):
        self.odometer_reading += miles
        
class ElectricCar(Car):#创建一个子类,继承父类
    #电动车的独特之处
    
    def __init__(self, make, model, year):#接受父类的信息
        #初始化父类的属性,super()函数帮助关联父类和子类
        super(). __init__(make, model, year)

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
2016 tesla model s
===============================================
###2.给子类定义属性和方法
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
    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 not roll back an odometer')
    def increment_odometer(self, miles):
        self.odometer_reading += miles
        
class ElectricCar(Car):#创建一个子类,继承父类
    #电动车的独特之处
    
    def __init__(self, make, model, year):#接受父类的信息
        #初始化父类的属性,super()函数帮助关联父类和子类
        super(). __init__(make, model, year)
        self.battery_size = 70#给子类定义属性
    def describe_battery(self):#给子类定义方法
        #打印一条描述电池容量的消息
        print('this car has ' + str(self.battery_size) + '-kwh battery')

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
2016 tesla model s
this car has 70-kwh battery
====================================================
###3.重写父类的方法
#对于父类的方法,只要它不符合子类模拟的实物的行为,都可对其重写。为此,可在子类中定义一个与要重写的父类方法同名,python将不会考虑父类方法,只关注子类中定义的相应方法
#假设Car类有一个名为fill_gas_tank()的方法,对电动车来说毫无意义,需要重新改写
class ElectricCar(Car):
    --snip--
    
    def fill_gas_tank():
        #电动汽车没有油箱
        print('this car does not need a gas tank')
================================================
###4.将实例用作属性,将类的一部分作为一个独立的类提取出来
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
    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 not roll back an odometer')
    def increment_odometer(self, miles):
        self.odometer_reading += miles

        
class Battery():#定义一个名为Battery的新类
    #模拟电动车的简单尝试
    def __init__(self, battery_size = 70):
    #初始化电瓶属性
        self.battery_size = battery_size
    
    def describe_battery(self):#给子类定义方法
        #打印一条描述电池容量的消息
        print('this car has ' + str(self.battery_size) + '-kwh battery')
        
class ElectricCar(Car):#创建一个子类,继承父类
    #电动车的独特之处
    
    def __init__(self, make, model, year):#接受父类的信息
        #初始化父类的属性,super()函数帮助关联父类和子类
        super(). __init__(make, model, year)
        self.battery = Battery()#添加一个名为self.battery的属性,并将Battery()实例存储在属性self.battery中
        

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
2016 tesla model s
this car has 70-kwh battery

2.导入类

python允许将类存储在模块中,然后在主程序中导入所需的模块

###1.导入单个类
#创建一个Car()类,存储在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
    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 not roll back an odometer')
    def increment_odometer(self, miles):
        self.odometer_reading += miles
2016 audi a4
this car has 23 miles on it
====================================
###2.在一个模块中存储多个类
#新建一个Car()类,存储在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
    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 not roll back an odometer')
    def increment_odometer(self, miles):
        self.odometer_reading += miles
        
class Battery():#定义一个名为Battery的新类
    #模拟电动车的简单尝试
    def __init__(self, battery_size = 70):
    #初始化电瓶属性
        self.battery_size = battery_size
    
    def describe_battery(self):#给子类定义方法
        #打印一条描述电池容量的消息
        print('this car has ' + str(self.battery_size) + '-kwh battery')
        
class ElectricCar(Car):#创建一个子类,继承父类
    #电动车的独特之处
    
    def __init__(self, make, model, year):#接受父类的信息
        #初始化父类的属性,super()函数帮助关联父类和子类
        super(). __init__(make, model, year)
        self.battery = Battery()#添加一个名为self.battery的属性,并将Battery()实例存储在属性self.battery中
2016 tesla model s
this car has 70-kwh battery
==========================================
###3.从一个模块中导入多个类
from car import Car, ElectricCar

my_beetle = Car('bmw', 'q7', 2016)
print(my_beetle.get_descriptive_name())

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
2016 bmw q7
2016 tesla model s
=============================================
###4.导入整个模块
import car

my_beetle = Car('bmw', 'q7', 2016)
print(my_beetle.get_descriptive_name())

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
2016 bmw q7
2016 tesla model s
============================================
###5.导入模块中的所有类
from module_name import *
======================================
###6.在一个模块中导入另一个模块
#将Car类存储在car.py文件中,将 ElectricCar和Battery存储在一个叫electric_car.py的文件中
#如何创建car.py文件
class Car():
    --snip--    
#如何创建electric_car.py文件
from car import Car#将Car类导入到该模块中,因为ElectricCar类需要访问其父类Car
class Battery():
    --snip--
    
class ElectricCar(Car);
    --snip--
#新建一个my_cars.py的文件
from car import Car
from electric_car import ElectricCar

my_beetle = Car('bmw', 'q7', 2016)
print(my_beetle.get_descriptive_name())

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
2016 bmw q7
2016 tesla model s
上一篇下一篇

猜你喜欢

热点阅读