# 案例classDog():'''一次模拟小狗的简单尝试'''def__init__(self,name,age):'''初始化属性name和age'''
self.name = name
self.age = age
defsit(self):'''模拟小狗被命令时蹲下'''print(self.name.title()+" is now sitting.")defroll_over(self):'''模拟小狗被命令时打滚'''print(self.name.title()+" rolled over!")
# 案例classDog():# 一次模拟小狗的简单尝试 def__init__(self, name, age):# 初始化属性 name 和 age
self.name = name
self.age = age
defsit(self):# 模拟小狗被命令时蹲下 print(self.name.title()+" is now sitting.")defroll_over(self):# 模拟小狗被命令时打滚 print(self.name.title()+" rolled over!")
my_dog = Dog('willie',6)print("My dog's name is "+ my_dog.name.title()+".")print("My dog is "+str(my_dog.age)+" years old.")
my_dog.name
My dog's name is Willie.
My dog is 6 years old.
'willie'
1.访问属性
通过实例访问的变量称为属性
使用句点表示法
2.调用方法
根据类创建实例后,可以使用句点表示法,调用Dog类中定义的任何方法
# 案例classDog():# 一次模拟小狗的简单尝试 def__init__(self, name, age):# 初始化属性 name 和 age
self.name = name
self.age = age
defsit(self):# 模拟小狗被命令时蹲下 print(self.name.title()+" is now sitting.")defroll_over(self):# 模拟小狗被命令时打滚 print(self.name.title()+" rolled over!")
my_dog = Dog('willie',6)
my_dog.sit()
my_dog.roll_over()
Willie is now sitting.
Willie rolled over!
3.创建多个实例
根据类创建任意数量的实例
# 案例classDog():# 一次模拟小狗的简单尝试 def__init__(self, name, age):# 初始化属性 name 和 age
self.name = name
self.age = age
defsit(self):# 模拟小狗被命令时蹲下 print(self.name.title()+" is now sitting.")defroll_over(self):# 模拟小狗被命令时打滚 print(self.name.title()+" rolled over!")
my_dog = Dog('willie',6)
your_dog = Dog('lucy',3)print("My dog's name is "+ my_dog.name.title()+".")print("My dog is "+str(my_dog.age)+" years old.")
my_dog.sit()print("\nYour dog's name is "+ your_dog.name.title()+".")print("My dog is "+str(your_dog.age)+" years old.")
your_dog.sit()
My dog's name is Willie.
My dog is 6 years old.
Willie is now sitting.
Your dog's name is Lucy.
My dog is 3 years old.
Lucy is now sitting.
练习
# 1.餐馆# 创建一个名为 Restaurant 的类,其方法 __init__() 设置两个属性: restaurant_name 和 cuisine_type 。创建一个名# 为 describe_restaurant() 的方法和一个名为 open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。# 根据这个类创建一个名为 restaurant 的实例,分别打印其两个属性,再调用前述两个方法。classRestaurant():def__init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name.title()
self.cuisine_type = cuisine_type
defdescribe_restaurant(self):print(self.restaurant_name +" server wonderful "+ self.cuisine_type +".")defopen_restaurant(self):print(self.restaurant_name +" is open. Come on in!")
restaurant = Restaurant('the mean queen','pizza')print(restaurant.restaurant_name)print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
The Mean Queen
pizza
The Mean Queen server wonderful pizza.
The Mean Queen is open. Come on in!
# 2.三家餐馆# 针对练习1,创建三个实例,并对每个实例调用方法 describe_restaurant()classRestaurant():def__init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name.title()
self.cuisine_type = cuisine_type
defdescribe_restaurant(self):print(self.restaurant_name +" server wonderful "+ self.cuisine_type +".")defopen_restaurant(self):print(self.restaurant_name +" is open. Come on in!")
restaurant1 = Restaurant('the mean queen','pizza')
restaurant2 = Restaurant('yong he','mi')
restaurant3 = Restaurant('li xian sheng','jiu')
restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()
The Mean Queen server wonderful pizza.
Yong He server wonderful mi.
Li Xian Sheng server wonderful jiu.