class Car():
"""一次魔力汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
def get_describle_name(self):
"""返回整洁的描述性信息"""
long_name = str(self.year) + " " + self.make + ' ' + self.model
return long_name.title() # str()函数强制将数字转为字符串
my_new_car = Car('audi', 'a4', '2016')
print(my_new_car.get_describle_name())
结果:
2016 Audi A4
class Car():
"""一次魔力汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 ## 添加一个随时间变化的属性
def get_describle_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_new_car = Car('audi', 'a4', '2016')
print(my_new_car.get_describle_name())
my_new_car.read_odometer()
结果:
2016 Audi A4
This car has 0 miles on it.
my_new_car = Car('audi', 'a4', '2016')
print(my_new_car.get_describle_name())
my_new_car.odometer_reading = 23 # 通过对象调用属性直接修改属性的值
my_new_car.read_odometer()
结果:
2016 Audi A4
This car has 23 miles on it.
class Car():
"""一次魔力汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 ## 添加一个随时间变化的属性
def update_odometer(self,mileage): # 更新里程属性的方法
"""将里程读数设置为指定的值"""
self.odometer_reading = mileage
def get_describle_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_new_car = Car('audi', 'a4', '2016')
print(my_new_car.get_describle_name())
结果:
2016 Audi A4
This car has 23 miles on it.
my_new_car.update_odometer(23)
my_new_car.read_odometer() # 调用显示里程的方法
结果:
This car has 23 miles on it
class Car():
"""一次魔力汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 ## 添加一个随时间变化的属性
def update_odometer(self,mileage): # 更新里程属性的方法
"""
将里程读数设置为指定的值
禁止任何人将里程属性往回调
只能加,不能减
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def get_describle_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.")
class Car():
"""一次魔力汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 ## 添加一个随时间变化的属性
# 更新里程属性的方法
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
def get_describle_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_used_car = Car('subaru', 'outback', 2013)
print(my_used_car.get_describle_name()) # 调用描述汽车属性的方法
my_used_car.update_odometer(23500) # 调用更新里程属性的方法
my_used_car.read_odometer() # 调用显示汽车里程的方法
my_used_car.increment_odometer(100) # 调用增加里程属性的方法
my_used_car.read_odometer() # 调用显示汽车里程属性的方法
结果:
2013 Subaru Outback
This car has 23500 miles on it.
This car has 23600 miles on it.
1、增加类的属性,并不一定需要在方法参数列表中有类属性的变量名。可以用 self. + 变量名的方式 = 默认值 的方式 来初始化
2、改变类属性的方法:1、直接通过实例进行修改;2、通过方法进行设置; 3、通过方法进行递增(增加特定的值)总之,通过实例访问属性来改或者在类中添加更改属性的方法(函数)