跳转至

Python入门

  • 在这里有Python语言学习的点滴,
  • 通过编写书籍📚内的课后代码,快速掌握基础知识🍀,
  • 快速查看对应代码,请看🫱目录。
  • 豆瓣简介: https://book.douban.com/subject/35196328/

创建日期:2024-06-09 | 更新日期:2024-06-09

注意💡: python2.7的代码开头需要加入字符编码声明,避免发生 SyntaxError: Non-ASCII character '\xe5' 错误🙅

Python
1
# -*- coding: utf-8 -*-

第9章 类

本章节的核心目标是熟悉 Python 语言中的类的属性、方法,以及如何使用和创建这些参数与函数。

Practice0901.py

题目9-1(p263):创建一个名为 Restaurant 的类,为其方法 init( ) 设置属性restaurant_name 和 cuisine_type。创建一个名为describe_restaurant( ) 的方法和一个名为open_restaurant( ) 的方法,前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。

根据这个类创建一$$个名为 restaurant 的实例,分别打印其两个属性,再调用前述两个方法。

practice0901.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# coding=utf-8

class Restaurant():

    def __init__(self,restaurant_name,cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print "The restaurant's name is {0}".format(self.restaurant_name)
        print "The restaurant's food style is {0}".format(self.cuisine_type)

    def open_restaurant(self):
        print "The restaurant is opening now."

if __name__ == '__main__':

    my_restaurant = Restaurant("厦门沙茶面", "福建特色小吃")
    my_restaurant.describe_restaurant()
    my_restaurant.open_restaurant()
Text Only
1
2
3
The restaurant's name is 厦门沙茶面
The restaurant's food style is 福建特色小吃
The restaurant is opening now.

类可以比喻为一种特定类别的统称,在本题中餐厅就是一个类,对应的 my_restaurant 就是一个具体的餐厅,在python中称为 类的实例

init( ) 方法自带一个形参self,将对应的餐厅属性 namecuisine type 都传入函数并初始化,在这个 Restaurant 类中同时给出了2个函数,由于此处是在类内,所以函数又被叫做 —— 方法。要注意调用不同的类的属性,实际上是通过类自带的 实参self 进行调用的,没错 self即是形参也是实参。

通过创建一个类的实例 my_restaurant,调用类里面的方法,完成了类的创建与使用。

Practice0902.py

题目9-2(p263)根据为完成练习9-1而编写的类创建三个 实例,并对每个实例调用方法 describe_restaurant( )。

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# coding=utf-8

class Restaurant():

    def __init__(self,restaurant_name,cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print "The restaurant's name is {0}".format(self.restaurant_name)
        print "The restaurant's food style is {0}".format(self.cuisine_type)

    def open_restaurant(self):
        print "The restaurant is opening now."

if __name__ == '__main__':

    p1_restaurant = Restaurant("沙县小吃", "微辣小炒菜")
    p1_restaurant.describe_restaurant()

    p2_restaurant = Restaurant("厦门沙茶面", "厦门特色小吃")
    p2_restaurant.describe_restaurant()

    p3_restaurant = Restaurant("蜜雪冰城", "夏日冰饮")
    p3_restaurant.describe_restaurant()
Text Only
1
2
3
4
5
6
The restaurant's name is 沙县小吃
The restaurant's food style is 微辣小炒菜
The restaurant's name is 厦门沙茶面
The restaurant's food style is 厦门特色小吃
The restaurant's name is 蜜雪冰城
The restaurant's food style is 夏日冰饮

Practice0903.py

题目9-3(p263): 创建一个名为 User 的类,其中包含属性 first_name 和 last_name,以及用户简介通常会存储的其他几个属性。1.在类 User 中定义一个名为 describe_user() 的方法,用于打印用户信息摘要。2.再定义一个名为 greet_user() 的方法,用于向用户发出个性化的问候。3.创建多个表示不同用户的实例,并对每个实例调用上述两个方法。

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# coding=utf-8

class User():

    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def describe_user(self):
        print "My name is {0} {1}.".format(self.first_name, self.last_name)

    def greet_user(self):
        print "Just wanted to let you know I’m thinking of you. Have a great day!"

if __name__ == '__main__':

    p1_user = User("Fanqie", "Lan")
    p1_user.describe_user()
    p1_user.greet_user()

    p2_user = User("Yichen", "Chen")
    p2_user.describe_user()
    p2_user.greet_user()

    p3_user = User("Keshu", "Yi")
    p3_user.describe_user()
    p3_user.greet_user()
Text Only
1
2
3
4
5
6
My name is Fanqie Lan.
Just wanted to let you know I’m thinking of you. Have a great day!
My name is Yichen Chen.
Just wanted to let you know I’m thinking of you. Have a great day!
My name is Keshu Yi.
Just wanted to let you know I’m thinking of you. Have a great day!