# 案例:classCar():'''一次模拟汽车的简单尝试'''def__init__(self,make,model,year):'''初始化描述汽车的属性'''
self.make = make
self.model = model
self.year = year
defget_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())
2016 Audi A4
给属性指定默认值
类中的每个属性都必须有初始值,哪怕这个值时0或空字符串
# 案例:classCar():'''一次模拟汽车的简单尝试'''def__init__(self,make,model,year):'''初始化描述汽车的属性'''
self.make = make
self.model = model
self.year = year
'''添加一个属性,初始值总是为0'''
self.odometer_reading =0defget_descriptive_name(self):'''返回整洁的描述信息'''
long_name =str(self.year)+' '+ self.make +' '+ self.model
return long_name.title()defread_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()
2016 Audi A4
This car has 0 miles on it
修改属性的值
以三种不同的方式修改属性的值:
1.直接通过实例进行修改
2.通过方法进行设置
3.通过方法进行递增(增加特定的值)
# 1.直接修改属性的值# 要修改属性的值,最简单的方式是通过实例直接访问它# 案例:classCar():'''一次模拟汽车的简单尝试'''def__init__(self,make,model,year):'''初始化描述汽车的属性'''
self.make = make
self.model = model
self.year = year
'''添加一个属性,初始值总是为0'''
self.odometer_reading =0defget_descriptive_name(self):'''返回整洁的描述信息'''
long_name =str(self.year)+' '+ self.make +' '+ self.model
return long_name.title()defread_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()
2016 Audi A4
This car has 23 miles on it
# 2.通过方法修改属性的值# 使用方法更新属性的值,将值传递给一个方法,由它在内部进行更新,无需直接访问属性,# 案例:classCar():'''一次模拟汽车的简单尝试'''def__init__(self,make,model,year):'''初始化描述汽车的属性'''
self.make = make
self.model = model
self.year = year
'''添加一个属性,初始值总是为0'''
self.odometer_reading =0defget_descriptive_name(self):'''返回整洁的描述信息'''
long_name =str(self.year)+' '+ self.make +' '+ self.model
return long_name.title()defread_odometer(self):'''打印一套指出汽车里程的消息'''print("This car has "+str(self.odometer_reading)+' miles on it')defupdate_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()
2016 Audi A4
This car has 23 miles on it
# 案例2classCar():'''一次模拟汽车的简单尝试'''def__init__(self,make,model,year):'''初始化描述汽车的属性'''
self.make = make
self.model = model
self.year = year
'''添加一个属性,初始值总是为20'''
self.odometer_reading =20defget_descriptive_name(self):'''返回整洁的描述信息'''
long_name =str(self.year)+' '+ self.make +' '+ self.model
return long_name.title()defread_odometer(self):'''打印一套指出汽车里程的消息'''print("This car has "+str(self.odometer_reading)+' miles on it')defupdate_odometer(self,mileage):'''
将里程表读数设置为指定的值
禁止将里程表读数往回调
'''if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:print("You can't roll back an odometer!")
my_new_car = Car('audi','a4',2016)print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(15)
my_new_car.read_odometer()
2016 Audi A4
You can't roll back an odometer!
This car has 20 miles on it
# 3.通过方法对属性的值进行递增# 有时需要将属性值递增特定的量,而不是将其设置为全新的值# 案例:classCar():'''一次模拟汽车的简单尝试'''def__init__(self,make,model,year):'''初始化描述汽车的属性'''
self.make = make
self.model = model
self.year = year
'''添加一个属性,初始值总是为0'''
self.odometer_reading =0defget_descriptive_name(self):'''返回整洁的描述信息'''
long_name =str(self.year)+' '+ self.make +' '+ self.model
return long_name.title()defread_odometer(self):'''打印一套指出汽车里程的消息'''print("This car has "+str(self.odometer_reading)+' miles on it')defupdate_odometer(self,mileage):'''
将里程表读数设置为指定的值
禁止将里程表读数往回调
'''if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:print("You can't roll back an odometer!")defincrement_odometer(self,miles):'''将里程表读数增加指定的量'''
self.odometer_reading += miles
my_userd_car = Car('subaru','outback',2013)print(my_userd_car.get_descriptive_name())
my_userd_car.update_odometer(23500)
my_userd_car.read_odometer()
my_userd_car.increment_odometer(100)
my_userd_car.read_odometer()
2013 Subaru Outback
This car has 23500 miles on it
This car has 23600 miles on it
练习
# 1.就餐人数:# 添加一个名为number_served 的属性,并将其默认值设置为 0 。根据这个类创建一个名为 restaurant 的实# 例,打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它添加一个名为 set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。# 添加一个名为 increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就# 餐人数。classRestaurant():def__init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name.title()
self.cuisine_type = cuisine_type
self.number_served =0print(self.restaurant_name +" server wonderful "+ self.cuisine_type +".")defdescribe_restaurant(self):print("How many people are in restaurant?"+'\t'+str(self.number_served))defopen_restaurant(self):print(self.restaurant_name +" is open. Come on in!")print("The numbers:"+ self.number_served)defset_number_served(self,number):
self.number_served = number
defincrement_number_served(self,add_number):
self.number_served += add_number
restaurant = Restaurant('the mean queen','pizza')
restaurant.describe_restaurant()
restaurant.set_number_served(10)
restaurant.describe_restaurant()
restaurant.increment_number_served(10)
restaurant.describe_restaurant()
The Mean Queen server wonderful pizza.
How many people are in restaurant? 0
How many people are in restaurant? 10
How many people are in restaurant? 20