赞
踩
目录
练习9-16:Python Module of the Week
- class Dog:
- """一次模拟小狗的简单尝试。"""
- def __init__(self, name, age):
- """初始化属性name和age。"""
- #❹
- self.name = name
- self.age = age
-
- def sit(self):
- """模拟小狗收到命令时蹲下。"""
- print(f"{self.name} is now sitting.")
-
- def roll_over(self):
- """模拟小狗收到命令时打滚。"""
- print(f"{self.name} rolled over!")
- class Dog:
- """一次模拟小狗的简单尝试。"""
- def __init__(self, name, age):
- """初始化属性name和age。"""
- self.name = name
- self.age = age
-
- def sit(self):
- """模拟小狗收到命令时蹲下。"""
- print(f"{self.name} is now sitting.")
-
- def roll_over(self):
- """模拟小狗收到命令时打滚。"""
- print(f"{self.name} rolled over!")
- #❶处
- my_dog = Dog('Willie', 6)
-
- #❷处
- print(f"My dog's name is {my_dog.name}.")
- #❸处
- print(f"My dog is {my_dog.age} years old.")
my_dog.name
- My dog's name is Willie.
- My dog is 6 years old.
- class Dog:
- --snip--
-
- my_dog = Dog('Willie', 6)
- my_dog.sit()
- my_dog.roll_over()
- Willie is now sitting.
- Willie rolled over!
- class Dog:
- --snip--
-
- my_dog = Dog('Willie', 6)
- your_dog = Dog('Lucy', 3)
-
- print(f"My dog's name is {my_dog.name}.")
- print(f"My dog is {my_dog.age} years old.")
- my_dog.sit()
-
- print(f"\nYour dog's name is {your_dog.name}.")
- print(f"Your dog is {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.
- Your dog is 3 years old.
- Lucy is now sitting.
- class Restaurant:
- """餐馆信息"""
- def __init__(self, restaurant_name, cuisine_type):
- """初始化属性"""
- self.restaurant_name = restaurant_name
- self.cuisine_type = cuisine_type
-
- def describe_restaurant(self):
- """餐馆信息"""
- print(f"This {self.restaurant_name}'s style is {self.cuisine_type}")
-
- def open_restaurant(self):
- """营业情况"""
- print("opening")
-
-
- my_restaurant = Restaurant('xiao mi fan dian', 'chuan cai')
- my_restaurant.describe_restaurant()
- my_restaurant.open_restaurant()
- my_restaurant = Restaurant('xiao mi fan dian', 'chuan cai')
- your_restaurant = Restaurant('huazhong fandian', 'xiang cai')
- his_restaurant = Restaurant('zhongnan fandian', 'yue cai')
- my_restaurant.describe_restaurant()
- your_restaurant.describe_restaurant()
- his_restaurant.describe_restaurant()
- class User:
- """用户信息"""
- def __init__(self, first_name, last_name, **info):
- """信息初始化"""
- self.first_name = first_name
- self.last_name = last_name
- self.info = info
-
- def describe_user(self):
- """描述用户信息"""
- print(f"{self.first_name.title()} {self.last_name.title()}")
- print(self.info)
-
- def greet_user(self):
- """打招呼"""
- print(f"Hello,{self.first_name.title()} {self.last_name.title()}!")
-
-
- first_person = User('bob', 'cruise', age='14', habit='eat')
- first_person.describe_user()
- first_person.greet_user()
- class Car:
- """一次模拟汽车的简单尝试。"""
- # ❶
- def __init__(self, make, model, year):
- """初始化描述汽车的属性。"""
- self.make = make
- self.model = model
- self.year = year
- # ❷
- def get_descriptive_name(self):
- """返回整洁的描述性信息。"""
- long_name = f"{self.year} {self.make} {self.model}"
- return long_name.title()
- # ❸
- my_new_car = Car('audi', 'a4', 2019)
- print(my_new_car.get_descriptive_name())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。