任务1.餐馆
- 问题描述
创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名 为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。根据这个类创建一个名为restaurant 的实例,分别打印其两个属性,再调用前述两个方法。 - 源代码
# coding:gbk
class Restaurant():
"""restaurant information"""
def __init__(self, restaurant_name, cuisine_type):
"""Init."""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
""""Returns clean description information."""
print("\n 餐厅名称: " + self.restaurant_name)
print("\n 菜式: " + self.cuisine_type)
def open_restaurant(self):
print("The restaurant is open now.")
restaurant = Restaurant('Hong Kong style tea restaurant', 'Roast goose rice')
restaurant.describe_restaurant()
restaurant.open_restaurant()
print("My favorite restaurant is " + restaurant.restaurant_name.title() +".")
print("The restaurant cuisine is " + restaurant.cuisine_type.title()+".")
- 结果截图
任务2.三家餐馆
- 问题描述
根据为完成练习任务1而编写的类创建三个实例,并对每个实例调用方法describe_restaurant() 。 - 源代码
# coding:gbk
class Restaurant():
"""restaurant information"""
def __init__(self, restaurant_name, cuisine_type):
"""Init."""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
""""Returns clean description information."""
print("\n 餐厅名称: " + self.restaurant_name)
print("\n 菜式: " + self.cuisine_type)
def open_restaurant(self):
print("The restaurant is open now.")
restaurant = Restaurant('Hong Kong style tea restaurant', 'Roast goose rice')
restaurant.describe_restaurant()
restaurant.open_restaurant()
print("My favorite restaurant is " + restaurant.restaurant_name.title() +".")
print("The restaurant cuisine is " + restaurant.cuisine_type.title()+".")
restaurant = Restaurant('Prince Restaurant', 'Chinese cuisine')
restaurant.describe_restaurant()
restaurant.open_restaurant()
print("My favorite restaurant is " + restaurant.restaurant_name.title() +".")
print("The restaurant cuisine is " + restaurant.cuisine_type.title()+".")
restaurant = Restaurant('Photos at Restaurant Teak Kee', 'Grill')
restaurant.describe_restaurant()
restaurant.open_restaurant()
print("My favorite restaurant is " + restaurant.restaurant_name.title() +".")
print("The restaurant cuisine is " + restaurant.cuisine_type.title()+".")
- 结果截图
任务3.用户
- 问题描述
创建一个名为User 的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名 为describe_user() 的方法,它打印用户信息摘要;再定义一个名为greet_user() 的方法,它向用户发出个性化的问候。 - 源代码
# coding:gbk
import hashlib
class User():
"""individual resume"""
def __init__(self, first_name, last_name, country):
self.first_name = first_name
self.last_name = last_name
self.country = country
def describe_user(self):
hash_md5 = hashlib.md5('data'.encode('utf-8'))
hash_md5.update('admin'.encode('utf-8'))
return hash_md5.hexdigest()
def greet_user(self):
print("Hello!")
user_aurora = User('Aurora', 'Emily', 'America')
print(user_aurora.first_name)
print(user_aurora.last_name)
print(user_aurora.country)
print(user_aurora.describe_user())
print(user_aurora.greet_user())
- 结果截图
知识点:
类和对象
1.对象是现实世界实体进行抽象化后以编程的方式的实现
2.类是对象的进一步抽象(分类)
3.类是对象的模板,对象是类的实例(实实在在的例子)
类属性
1.数据属性
定义类的变量。这些变量可以像任何其他变量一样在类创建后可以被使用。这样的变量也称为静态变量。类的所有实例共享静态变量。
2.方法属性
定义类的函数操作。这些方法可以通过类或者实例化后的对象进行调用。
实例属性
属性要点
- 实例仅仅拥有数据属性
- 方法属性严格来说是类属性
- 通过句点标识来访问属性
- Python能够在“运行时”创建实例属性,慎用
构造函数
- 所有的构造函数都会用到__init__()
- 构造函数是最早设置实例属性的地方
- init()就当返回None