Python 学习笔记14 类 - 使用类和实例

当我们熟悉和掌握了怎么样创建类和实例以后,我们编程中的大多数工作都讲关注在类的简历和实例对象使用,修改和维护上。

结合实例我们来进一步的学习类和实例的使用:

我们新建一个汽车的类:

#-*- coding:utf-8 -*-

class Car():

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def get_description_name(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()



my_car = Car("audi", "A4", 2016)
print(my_car.get_description_name())

'''
输出:
2016 Audi A4
'''

 

通过上面的例子我们可以看到,我们创建了一个car的类,这个类有三个参数,并且有一个打印所有信息的一个方法。

我们知道其实每个属性都有初始的值,有些时候我们的属性并不是为空或者0,他需要一个默认的其他值。我们可以在类的__init__方法中,对一些属性进行初始化。一旦属性具有初始值后,在实例化对象时,可以不提供数值。

我们在类中添加一个里程表的属性,他的初始值应该为0,我们添加一个方法用来读取这个属性。

#-*- coding:utf-8 -*-

class Car():

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_description_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.")

my_car = Car("audi", "A4", 2016)
print(my_car.get_description_name())
my_car.read_odometer()

'''
输出:
2016 Audi A4
This car has 0 miles on it.
'''

 

我们可以直接修改实例的属性,来实现我么预想的操作:

#-*- coding:utf-8 -*-

class Car():

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_description_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.")

my_car = Car("audi", "A4", 2016)
print(my_car.get_description_name())
my_car.read_odometer()
my_car.odometer_reading = 100
my_car.read_odometer()
'''
输出:
2016 Audi A4
This car has 0 miles on it.
This car has 100 miles on it.
'''

 

也可以通过添加特定方法来实现对实例属性的修改:

#-*- coding:utf-8 -*-

class Car():

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_description_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):
        self.odometer_reading = mileage


my_car = Car("audi", "A4", 2016)
print(my_car.get_description_name())
my_car.read_odometer()
my_car.update_odometer(200)
my_car.read_odometer()
'''
输出:
2016 Audi A4
This car has 0 miles on it.
This car has 200 miles on it.
'''

 

我么知道里程数一般都是递增的,正常情况下我们是无法将其往回调整的,所以我们需要对update函数进一步的优化。输入的参数必须大于等于当前值,小于则必须报错:

#-*- coding:utf-8 -*-

class Car():

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_description_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 cannot do that.")


my_car = Car("audi", "A4", 2016)
print(my_car.get_description_name())
my_car.read_odometer()

my_car.update_odometer(200)
my_car.read_odometer()

my_car.update_odometer(100)
my_car.read_odometer()
'''
输出:
2016 Audi A4
This car has 0 miles on it.
This car has 200 miles on it.
You cannot do that.
This car has 200 miles on it.
'''

 

 

根据现实中的情况,其实我们定义一个增量方法更为合适,该方法每次都加上参数给定的值,并且该值应该大于等于0:

#-*- coding:utf-8 -*-

class Car():

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_description_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 cannot do that.")

    def increase_odometer(self, miles):
        if miles >= 0:
            self.odometer_reading += miles
        else:
            print("The value is invalid, please input the number which should more than zero.")


my_car = Car("audi", "A4", 2016)
print(my_car.get_description_name())
my_car.read_odometer()

my_car.update_odometer(200)
my_car.read_odometer()

my_car.increase_odometer(100)
my_car.read_odometer()

my_car.increase_odometer(-1)
my_car.read_odometer()
'''
输出:
2016 Audi A4
This car has 0 miles on it.
This car has 200 miles on it.
This car has 300 miles on it.
The value is invalid, please input the number which should more than zero.
This car has 300 miles on it.
'''

 

posted @ 2019-05-19 20:58  追风狂想  阅读(166)  评论(0编辑  收藏  举报